[LCP]passing struct to function

Carlos Pruitt, Jr. cept at bellsouth.net
Sun Nov 21 21:39:02 UTC 2004


Thanks to everyone who replied.  It makes perfect sense now.  I am 
almost halfway through Oreilly's Practical C Programming.  This is the 
first real problem I have had.  It looks like I attempted to do a 
mixture of both methods :P

As Greg pointed out I could have simply passed the address of my list to 
the function or I could have declared a pointer, assigned the address of 
my list to the pointer, then passed the pointer (not address to the 
pointer) to the function.  I have tested both methods and they work fine.

Which of the two methods is preferred in the industry?  Or does it 
really matter, as long as I am consistant?

Thank again,

carlos

 

Greg Black wrote:

>On 2004-11-20, Carlos Pruitt, Jr. wrote:
>  
>
>>I am trying to find what I am doing wrong.  This program compiles with 
>>the warning message:
>>
>>warning: passing arg 1 of `print_mailing_list' from incompatible pointer 
>>type
>>
>>When I run the program, the output is:
>>
>>f at carlos
>>
>>
>>#include <stdio.h>
>>#include <string.h>
>>
>>struct mailing
>>{
>>       char first_name[60];
>>};
>>
>>void print_mailing_list(struct mailing *mlist)
>>{
>>       printf("%s\n", mlist->first_name);
>>}
>>
>>int main()
>>{
>>       struct mailing list =
>>               {
>>                       "carlos"
>>               };
>>
>>       struct mailing *list_ptr;
>>    
>>
>
>Delete the line above; you don't use this variable anywhere so
>it is not needed.
>
>  
>
>>       if (strcmp(list.first_name, "carlos") == 0)
>>               print_mailing_list(&list_ptr);
>>    
>>
>
>Change the above to:
>
>                 print_mailing_list(&list);
>
>  
>
>>       return 0;
>>
>>}
>>    
>>
>
>Compile, test.  Then study and understand.
>
>Greg
>
>_______________________________________________
>This is the Linux C Programming List
>:  http://lists.linux.org.au/listinfo/linuxcprogramming List
>
>
>  
>


-- 
Carlos E. Pruitt, Jr.
cept at bellsouth.net

Oliver's Law:
Experience is something 
you don't get until just 
after you need it.





More information about the linuxCprogramming mailing list