Thread: Struct pointer problem

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

    Struct pointer problem

    I am facing a problem involving a pointer to a struct here, and i'm not sure if i'm getting the concept right...

    i've attached the zip file, once unzipped, open the startup.c file and after compiling it, enter 'Francis' for username and 'Kheng' for password...

    for menu, choose number 4, and for the next submenu, choose 1.

    then for intake, type 'hf0271c'

    and then press to see some text on the screen followed by an error message which will pop up...

    this error can be traced to file reporting.h

    under the displayReporting() function i hvae commented the problem, which involves a pointer to a struct...did i declare it properly, and am i allowed to use it in such a way? as i've used it elsewhere and it worked, w hereas now it makes the program hang...with an error message...
    Only by the cross are you saved...

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    to summarise my problem, i declared a pointer to a struct like this...

    ACADEMICPROF *details;

    then, in a function, i immediately assigned a value to the data member of the struct type it points to...

    details->studentID[j] = *resultFields[i]; //this is a character assignment

    is this wrong since i have not assigned any address to this pointer yet?

    but this method did not give me problems when i used it earlier in my SESFunctions.h file...just that i substituted the struct name for USERPROF and the pointer name as *prof
    Only by the cross are you saved...

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    102
    In your reporting.h make this change.
    Code:
    struct ACADEMICPROF{
    	char studentID[27];
    	int semesterNumber;
    	char subject[10];
    	float incourse;
    	float exam;
    	float overall;
    	struct ACADEMICPROF *next;
    };
    Is it working now?
    Saravanan.T.S.
    Beginner.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    nope, if i take away that one, i get many errors, some of which state the i have a missing identifier ACADEMICPROF...
    Only by the cross are you saved...

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    102
    Oh!! Dear. just declare one structure of that type.
    Saravanan.T.S.
    Beginner.

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    sorry, but wat do u mean by that?
    Only by the cross are you saved...

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    With this:
    >>details->studentID[j]= *resultFields[i];
    are you trying to copy the string? If so, you need strcpy().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    yeap, i am trying to assign or copy, so i have to use strcpy()?

    i'll try, thank you!
    Only by the cross are you saved...

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    okay, yeah i know, coz i rush into things without planning...

    anyway, i tried with strcpy but it still didn't work...i really do not know wat the problem is...
    Only by the cross are you saved...

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>anyway, i tried with strcpy but it still didn't work...i really do not know wat the problem is
    Then you didn't use it properly. Try writing a small program using those structs, just to get used to using strcpy on them. Post it here when you have trouble.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    to summarise my problem, i declared a pointer to a struct like this...

    ACADEMICPROF *details;

    then, in a function, i immediately assigned a value to the data member of the struct type it points to...

    details->studentID[j] = *resultFields[i]; //this is a character assignment

    is this wrong since i have not assigned any address to this pointer yet?
    Yes. To avoid this problem you may want to prepend pointer names with p eg. pAcProfDetails. This tells you it is a pointer to an ACADEMICPROF struct. You must initialise a pointer before dereferencing it.

    but this method did not give me problems when i used it earlier in my SESFunctions.h file...just that i substituted the struct name for USERPROF and the pointer name as *prof
    Ok, so what is this?
    Code:
    prof = (USERPROF *) malloc (sizeof (USERPROF));
    Looks like initialisation of prof to my simple mind.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to struct
    By Paul Johnston in forum C Programming
    Replies: 4
    Last Post: 06-11-2009, 03:01 AM
  2. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  3. A problem with pointer initialization
    By zyklon in forum C Programming
    Replies: 5
    Last Post: 01-17-2009, 12:42 PM
  4. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM