Thread: pointers to struct, can't get the hang of it....

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    43

    pointers to struct, can't get the hang of it....

    hi,
    i'm trying to understand the use of pointer to structs, calling by address, etc.
    can get the declarations right:
    Code:
    month* AddMonth(month *monthPtr,month **lastMonth,char[MONTHCODE] name)
    {
    	//allocate month in mem
    	monthPtr=(month*)realloc(month,sizeof(month));
    	//check
    	CheckAlloc((void*)*monthPtr);
    	//move last month pointer
    	(*lastMonth)++;
    	//add new month name to newly created month
    	MyStrcpy((*lastMonth)->name,name);
    	return monthPtr;
    }
    the declaration of this function and the structs:
    Code:
    month* AddMonth(month *monthPtr,month **lastMonth,char[MONTHCODE] name);
    typedef struct {
    	int dayNum;
    	int numOfTasks;
    	char *task;
    } day;
    typedef struct {
    	char name[3];
    	day *dayInMonth;
    } month;
    the compiler is saying things like: syntax error : missing '{' before '*'

    not sure what i've done wrong
    HELP!
    thanks
    Last edited by msshapira; 01-16-2009 at 03:00 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    month* AddMonth(month *monthPtr,month **lastMonth,char[MONTHCODE] name);
    Should be:
    Code:
    month* AddMonth(month* monthPtr, month** lastMonth, char name[MONTHCODE]);
    Also, I'm not so sure this is a good idea:
    Code:
    CheckAlloc((void*)*monthPtr);
    You cast a non-pointer value to a void* pointer.
    Furthermore, if the CheckAlloc is supposed to check the pointer, why are you dereferencing it?
    If it checks if it's a valid month or so, then why is it taking a void* pointer?
    That function is ambiguous to me.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    43
    the way i see it, the code you wrote and the one i wrote are the same thing:
    int* pointer, is the same as int *pointer

    CheckAlloc((void*)*monthPtr); checks the allocation is successful, this function is to be used with all types of pointer and therefore needs casting... (am i wrong?)

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Nope, it's not the same. Check the last one - the name char array. The size comes after the name.
    If it works with pointers, then why do you dereference the pointer before passing it? That makes it a pointer no longer.
    And how the compiler is supposed to pass a struct as a void* pointer is beyond me, frankly.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    43
    either way, it's not working.
    any idea?
    error is in the declarations

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    typedef struct
    {
    	int dayNum;
    	int numOfTasks;
    	char* task;
    } day;
    
    typedef struct
    {
    	char name[3];
    	day* dayInMonth;
    } month;
    
    month* AddMonth(month* monthPtr, month** lastMonth, char name[MONTHCODE]);
    How about this then?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    43
    thanks... how low can i stoop

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  2. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  3. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  4. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 11:50 PM
  5. Pointers on pointers to pointers please...
    By Morgan in forum C Programming
    Replies: 2
    Last Post: 05-16-2003, 11:24 AM