Thread: why this program display this error:

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    10

    Question why this program display this error:

    why this program display this error:
    [6 main.c two or more data types in declaration of `info_input']

    please help me with this problem , source code as following.

    //source code :

    //file main.c
    #include <stdio.h>
    #include <my_lib.h>


    void info_input()
    {
    struct tongxun *p;
    p = (struct tongxun*)malloc(sizeof(struct tongxun));
    p->id = "97100456";
    p->name = "rochelle hsu";
    p->email = "[email protected]";
    p->oicq = "17731056";
    free(p);
    return;
    }

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

    //input personal infomation
    info_input();

    //printf("%s",p->name);


    return 0;
    }

    //file my_lib.c

    //define the struct tongxun
    struct tongxun
    {
    char *id;
    char *name;
    char *email;
    char *oicq;
    char *home_phone;
    char *mobile_phone;
    }

    mail me to : rjhome

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4
    Hi rjhome,

    It looks like the problem might be that you have a structure (tongxun) of uninitialized pointers to type char. When you allocate memory for a structure in your code

    Code:
    void info_input() 
    { 
    struct tongxun *p; 
    p = (struct tongxun*)malloc(sizeof(struct tongxun)); 
    p->id = "97100456"; 
    p->name = "rochelle hsu"; 
    p->email = "[email protected]"; 
    p->oicq = "17731056"; 
    free(p); 
    return; 
    }
    the pointer p ends up with only enough memory to hold the pointers which are up to this point uninitialized. I would suggest either one of two solutions, the first is defining your structure using character arrays ...

    Code:
    //define the struct tongxun 
    struct tongxun 
    { 
    char id[10]; 
    char name[MAX_NAME]; /* you could even use constants */
    char email[MAX_EMAIL]; 
    char oicq[MAX_ICQ]; 
    char home_phone[10 + 1];  /* standard number plus NULL */
    char mobile_phone[10 + 1]; 
    };      /* you forgot to close the structure with a semicolon */
    or you can allocate the memory dynamically in info_input() ...

    Code:
    void info_input() 
    {
        struct tongxun *p;
        char *idtmp = "97100456"; 
    
        if ((p->id = (char *)malloc(strlen(idtmp) + 1)) != NULL)
             strcpy(p->id, idtemp);
    
        ... and so forth ...
    This is not elegant but it does allocate sufficient memory for the stirng and not just the pointer.

    I hope this helps.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    10

    thanks

    Very thx for your reply,First.

    And I will tell u that the first solution is not valible.
    I've tested the first one before you suggest me,but it dosen't work.

    I will try the second one.

    I will reply again whether it can work or not.

    Thanks for your help.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    10

    Re: I found the problem:

    That is a small problem due to my carelessness.
    in my_lib.h
    Orign: struct tongxun {}
    Now : struct tongxun {};
    Pay attention to the last "; ",that's the problem.
    Hehe,Thx for ur help anyway.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Well, you DO initialize the character pointers in this example, BUT if you wanted to read user input, and store THAT input in your struct, you must explicitly allocate memory for each string.

    Your current code works because when you have:

    char * char_ptr = "string literal";

    space for the string literal is automatically allocated, and the pointer points at the already allocated space. If, however, you tried to read user input into one of your char *'s, you'd segfault unless you allocated memory for that string first.
    Last edited by The V.; 10-05-2001 at 09:32 AM.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    10

    thx V.

    Thank you for your teaching.
    I have noticed that problem.
    And I 've felt that deeply by myself.In this morning I get a lesson of this not allocing a memory for it.And the compiler tell it wrong.
    I am a beginner of the c .
    And I am an expert programmer in Perl.
    I turned to c language just a little time.
    I want to get help from u all.
    And I thank for your hlep.
    By the way, I come from China.And not good English Level.
    But I like programming in Perl and C.
    Hoping for ur answers while I ask questions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM