Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: How is it w...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 5 Topic 26072 of 26972
Post > Topic >>

Re: How is it working? Am I doing it right?

by Keith Thompson <kst-u@[EMAIL PROTECTED] > May 5, 2008 at 09:37 AM

Benoit Lefebvre <benoit.lefebvre@[EMAIL PROTECTED]
> 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@[EMAIL PROTECTED]
>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
 




 5 Posts in Topic:
How is it working? Am I doing it right?
Benoit Lefebvre <benoi  2008-05-05 08:54:09 
Re: How is it working? Am I doing it right?
roberson@[EMAIL PROTECTED  2008-05-05 16:11:48 
Re: How is it working? Am I doing it right?
Keith Thompson <kst-u@  2008-05-05 09:39:15 
Re: How is it working? Am I doing it right?
Keith Thompson <kst-u@  2008-05-05 09:37:46 
Re: How is it working? Am I doing it right?
"Jim Langston"   2008-05-05 22:54:32 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 21:35:25 CDT 2008.