Thread: Structure Within Structure

  1. #1
    Registered User
    Join Date
    Nov 2003

    Structure Within Structure

    I am learning C from Deitel & Deitel''s How To Program book. I am trying to learn how to create a structure within a structure.

    I want the user to key in the particulars like name and age, etc and use the pointers and arrow pointers to refer to the data input in the structure.

    I have not finished coding yet and I faced this problem with my code.

    struct student {
    	char Name[20];
    	char Surname[20];
    	int studentNumber;
    	struct {
    		char telNumber[10];
    		char Address[50];
    		} personal;
    } studentRecord, *studentPtr;
    int main(void) 
    	studentPtr = &studentRecord;
    	printf("Enter student name: \n");
    	scanf("%c", &Name);
    	printf("Enter student surname: \n");
    	scanf("%c", &Surname);
    	return (0);

    I haven't finished coding yet but when I compiled I received this message in my Mocrosoft Visual C++ compiler :

    error C2065: 'Name' : undeclared identifier.

    What is wrong with my "scanf("%c", &Name);" ?

    Should I declare char Name [20] in main again after declaring it in the structure ?

    I appreciate your help very much as I have no one to consult. I am learning C on my own.

  2. #2
    Registered User
    Join Date
    Aug 2003
    u need to dereference it using the structure name

    	printf("Enter student name: \n");
    	scanf("%c", &studentPtr->Name);
    	printf("Enter student surname: \n");
    	scanf("%c", &studentPtr->Surname);

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Waterloo, Texas
    Start with the basics, first:

    #define MAXLEN 128
    struct Name
     char first[MAXLEN];
     char last[MAXLEN];
    struct Person
     struct Name name;
     int age;
    int main()
     struct Person p;
     strcpy(, "Shakira");
     strcpy(, "Patel"); 
     p.age = 16;
     int * age = &p.age;
     char * first =;
     char * last =;
     printf("Hi, I'm %s %s and I am %d", 
     first, last, *age);
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
        return std::pow
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;

  4. #4
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Or, to rewrite Sebastiani's excellent example in my preferred method (which is obviously the _right_ way to do it-- grin):

    #define MAXLEN 128
    typedef struct NameStruc
       char  first[MAXLEN];
       char  last[MAXLEN];
    typedef struct PersonStruc
        Name  name;
        int       age;
    int main()
       Person   p;
       int          *age;
       char       *first;
       char       *last;
       strcpy(, "Shakira");
       strcpy(, "Patel"); 
       p.age = 16;
       age = &p.age;
       first =;
       last =;
       printf("Hi, I'm %s %s and I am %d",first, last, age);
    It is not the spoon that bends, it is you who bends around the spoon.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Replies: 5
    Last Post: 02-14-2006, 09:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM