Thread: Allocating Memory for a Structure

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    9

    Allocating Memory for a Structure

    Im just learning C and I need help. Im trying to allocate memory for a structure call mystruct, initialize the values for the strucutre and then return a pointer to the allocated memory:

    void main(){
    struct mystruct pdemo;
    pdemo=alloc_mystruct(buf1,buf2,val1);
    }

    struct mystruct *alloc_mystruct(char *name,const char *id,int val){
    struct mystruct *pMyStruct;

    pMyStruct->name=(char*)malloc(sizeof(name));
    pMyStruct->id=(char*)malloc(sizeof(id));
    pMyStruct->val=malloc(sizeof(val));

    strcpy(pMyStruct->name, name);
    strcpy(pMyStruct->id, id);
    pMyStruct->val=val;

    return pMyStruct;
    }

    could someone please point out what im doing wrong here?

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    9
    struct mystruct
    {
    char *name;
    char id[10];
    int val;
    };

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    9
    and the declaration should be:
    struct mystruct *pdemo;

    in the main function

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    Greetings,

    First of all: use code tags:
    Code:
    void main(){
        struct mystruct *pdemo; /* You forgot the '*', typo? */
        /* Also, at this point the compiler does not know about 'alloc_mystruct' it will assume it returns an int */
        pdemo=alloc_mystruct(buf1,buf2,val1);
    }
    
    struct mystruct *alloc_mystruct(char *name,const char *id,int val){
       struct mystruct *pMyStruct;
    
        pMyStruct->name=(char*)malloc(sizeof(name));
        pMyStruct->id=(char*)malloc(sizeof(id));
        pMyStruct->val=malloc(sizeof(val));
    
        strcpy(pMyStruct->name, name);
        strcpy(pMyStruct->id, id);
        pMyStruct->val=val;
    
        return pMyStruct;
    }
    Second, its 'int main', not 'void main'.
    Third, provides the full code, like where's buf1, buf2, val1, MyStruct defined and how?

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    9
    thanks, everything is working fine now...the function is already prototyped in the header file...the variables are being read in from console window....sorry to leave out so much...the project is rather large and i was trying to minimize the amount of code i posted in the forum...

    one last question

    is it necessary to use memset to initialize the structure after the memory has been allocated?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM
  2. allocating memory
    By viaxd in forum C Programming
    Replies: 2
    Last Post: 10-12-2003, 07:13 PM
  3. Allocating memory for a structure member
    By dalek in forum C++ Programming
    Replies: 2
    Last Post: 05-15-2003, 06:56 AM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM