Structure Within Structure

This is a discussion on Structure Within Structure within the C Programming forums, part of the General Programming Boards category; I am learning C from Deitel & Deitel''s How To Program book. I am trying to learn how to create ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    1

    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.

    Code:
    #include<stdio.h>
    
    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
    Posts
    51
    u need to dereference it using the structure name

    Code:
    	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
    Location
    Waterloo, Texas
    Posts
    5,708
    Start with the basics, first:

    Code:
    #define MAXLEN 128
    
    struct Name
    {
     char first[MAXLEN];
     char last[MAXLEN];
    };
    
    struct Person
    {
     struct Name name;
     int age;
    };
    
    int main()
    {
     struct Person p;
    
     strcpy(p.name.first, "Shakira");
     
     strcpy(p.name.last, "Patel"); 
    
     p.age = 16;
    
     int * age = &p.age;
     char * first = p.name.first;
     char * last = p.name.last;
    
     printf("Hi, I'm %s %s and I am %d", 
     first, last, *age);
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            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
    Posts
    212
    Or, to rewrite Sebastiani's excellent example in my preferred method (which is obviously the _right_ way to do it-- grin):

    Code:
    #define MAXLEN 128
    
    typedef struct NameStruc
       {
       char  first[MAXLEN];
       char  last[MAXLEN];
       }Name;
    
    typedef struct PersonStruc
       {
        Name  name;
        int       age;
       }Person;
    
    int main()
       {
       Person   p;
       int          *age;
       char       *first;
       char       *last;
    
       strcpy(p.name.first, "Shakira");
     
       strcpy(p.name.last, "Patel"); 
    
       p.age = 16;
    
       age = &p.age;
       first = p.name.first;
       last = p.name.last;
    
       printf("Hi, I'm %s %s and I am %d",first, last, age);
      }
    enjoy.
    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, 06: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, 07: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21