Thread: How i use a struct field on a function?

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    13

    How i use a struct field on a function?

    Hi, i'm trying to use a struct field on a function (by reference). i will show a example:

    Struct declaration:
    Code:
    typedef struct{
    	char teste1[3+1];
    	char teste2[4+1];
    }struct2;
    function:
    Code:
    void TrataStruct(char* parametro)
    {
    	printf("%s",parametro);
    }
    calling:
    Code:
    TrataStruct(&structTeste.teste1);
    error:

    error C2664: 'TrataStruct' : cannot convert parameter 1 from 'char (*)[4]' to 'char'.


    How i make it works?

    thx.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Should call with no '&'

    Code:
    TrataStruct(structTeste.teste1);
    structTeste.teste1 itself 'points to the base address of the your char array.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Assuming that structTeste is a valid struct object you just do TrataStruct(structTeste.teste1);
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    13
    1 - i Tried to use wthout '&' but it results in the same error
    2 - This is a example, on the real case the struct will not be visible to other function

    i dont know what to do, i tried to use (char parametro[3+1]) too,but it still wrong =/

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Post your code so we can see exactly what you are doing.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    13
    Code:
    #include "stdafx.h"
    
    void TrataStruct(char parametro[3+1]);
    int _tmain(int argc, _TCHAR* argv[])
    {
    	
    	typedef struct{
    	char teste1[3+1];
    	char teste2[4+1];
    	}struct2;
    	
    	struct2 structTeste;
    	
    	printf("%s","Test Value \n");
    	scanf("%s",&structTeste.teste1);
    	TrataStruct(&structTeste.teste1);
    	
    	
    	return (0);
    }
    void TrataStruct(char parametro[3+1])
    {
    	printf("%s",parametro);
    }

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    THIS: scanf("%s",&structTeste.teste1)

    Should be scanf("%s",structTeste.teste1) without the &. structTeste.teste1 is already a pointer to that string.

    TrataStruct should be called liked indicated above without the &.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM