PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > How is it working? Am I doing it right?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
How is it working? Am I doing it right?

Réponse
 
LinkBack Outils de la discussion
Vieux 05/05/2008, 16h54   #1
Benoit Lefebvre
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How is it working? Am I doing it right?

Weird subject you will say.. I didn't know what to put in there..

Long story short, I'm changing the value of a variable from a
function.. Nothing unusual there..

How come the value of the "word" variable change when using the
testfunct function? Am I sending a pointer to the word variable to
this function or whatever ??

I know it's a weird question.. especially that this is doing exactly
what I need.. but I want to know if I'm doing it right and if not, how
to make it work correctly.

The program where this is going is checking a configuration text file
and puts all the lines in a 2d char array for comparison later. Here
is a simplified "extraction" of the code.

------
#include <stdio.h>
#include <strings.h>

int testfunct (char *myword)
{
strcpy(myword,"hello");

return 0;
}

int main (int argc, char *argv[])
{
char word[100];

strcpy(word,"weeee");
printf("WORD: %s\n", word);
testfunct(word);
printf("WORD: %s\n", word);

return 0;
}
------
  Réponse avec citation
Vieux 05/05/2008, 17h11   #2
Walter Roberson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How is it working? Am I doing it right?

In article <d009b12e-13ff-44bd-a75b-b66a58854ca0@27g2000hsf.googlegroups.com>,
Benoit Lefebvre <benoit.lefebvre@gmail.com> wrote:

>How come the value of the "word" variable change when using the
>testfunct function? Am I sending a pointer to the word variable to
>this function or whatever ??


>int testfunct (char *myword)
>{
> strcpy(myword,"hello");
>
> return 0;
>}


>int main (int argc, char *argv[])
>{
> char word[100];
>
> strcpy(word,"weeee");
> printf("WORD: %s\n", word);
> testfunct(word);


When you pass an array name as a function parameter, it gets silently
converted into a pointer to the first element of the array.

> printf("WORD: %s\n", word);
>
> return 0;
>}


So yes, the above is fine / legal / well-defined, as long as testfunct()
does not write more characters into myword than the object is defined
to hold.
--
"The art of storytelling is reaching its end because the epic
side of truth, wisdom, is dying out." -- Walter Benjamin
  Réponse avec citation
Vieux 05/05/2008, 17h37   #3
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How is it working? Am I doing it right?

Benoit Lefebvre <benoit.lefebvre@gmail.com> writes:
> Weird subject you will say.. I didn't know what to put in there..
>
> Long story short, I'm changing the value of a variable from a
> function.. Nothing unusual there..
>
> How come the value of the "word" variable change when using the
> testfunct function? Am I sending a pointer to the word variable to
> this function or whatever ??
>
> I know it's a weird question.. especially that this is doing exactly
> what I need.. but I want to know if I'm doing it right and if not, how
> to make it work correctly.
>
> The program where this is going is checking a configuration text file
> and puts all the lines in a 2d char array for comparison later. Here
> is a simplified "extraction" of the code.
>
> ------
> #include <stdio.h>
> #include <strings.h>
>
> int testfunct (char *myword)
> {
> strcpy(myword,"hello");
>
> return 0;
> }
>
> int main (int argc, char *argv[])
> {
> char word[100];
>
> strcpy(word,"weeee");
> printf("WORD: %s\n", word);
> testfunct(word);
> printf("WORD: %s\n", word);
>
> return 0;
> }


Yes, you're sending a pointer to the function.

``word'' is an array object, so the argument to testfunct is an
expression of array type, which is implicitly converted to a pointer
to the array's first element. testfunct() then modifies the data that
its argument points to.

Read section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

--
Keith Thompson (The_Other_Keith) <kst-u@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  Réponse avec citation
Vieux 05/05/2008, 17h39   #4
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How is it working? Am I doing it right?

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
[...]
> When you pass an array name as a function parameter, it gets silently
> converted into a pointer to the first element of the array.

[...]

Yes. This is, of course, just one instance of a more general rule
having nothing to do with functions.

--
Keith Thompson (The_Other_Keith) <kst-u@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  Réponse avec citation
Vieux 06/05/2008, 06h54   #5
Jim Langston
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How is it working? Am I doing it right?

Benoit Lefebvre wrote:
> Weird subject you will say.. I didn't know what to put in there..
>
> Long story short, I'm changing the value of a variable from a
> function.. Nothing unusual there..
>
> How come the value of the "word" variable change when using the
> testfunct function? Am I sending a pointer to the word variable to
> this function or whatever ??
>
> I know it's a weird question.. especially that this is doing exactly
> what I need.. but I want to know if I'm doing it right and if not, how
> to make it work correctly.
>
> The program where this is going is checking a configuration text file
> and puts all the lines in a 2d char array for comparison later. Here
> is a simplified "extraction" of the code.
>
> ------
> #include <stdio.h>
> #include <strings.h>
>
> int testfunct (char *myword)


testfunct takes as a parameter a character pointer. A pointer to a
character or array of characters depending on how you use it.

> {
> strcpy(myword,"hello");


strcpy will copy into the location of the character pointer the contents of
the 2nd parameter.

>
> return 0;
> }
>
> int main (int argc, char *argv[])
> {
> char word[100];


word is defined as an array of 100 characters. &word[0] is the address of
the first character, but so is word by itself. It gets silently converted.

>
> strcpy(word,"weeee");


here word is silently converted to a char pointer, the same as &word[0]
which is where strcpy will start copying the contents of the 2nd parameter.

> printf("WORD: %s\n", word);
> testfunct(word);


here testfunct takes a char pointer so the compiler silently converts word
to a char pointer, the same as &word[0]

> printf("WORD: %s\n", word);
>
> return 0;
> }
> ------


It is a well formed program.

Note, however, that you may wish to pass a 2nd parameter to your function,
the size of the buffer.

If you had done:
char word[2];
testfunct(word);

The compiler would accept it and when you ran it you would get a buffer
overflow because strcpy doesn't know how long the buffer the char pointer is
pointing to, it has no way to know. So it mindlessly copies until it
reaches the terminating null character in the copy from buffer, effectively
trying to stuff 10 pounds of stuff in a 5 pound bag.


--
Jim Langston
tazmaster@rocketmail.com


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h33.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11906 seconds with 13 queries