Thread: struct ptrs to struct ptrs

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    183

    struct ptrs to struct ptrs

    i just wanted to ask if i can like do from struct ptrs to struct ptrs like
    Code:
    #include <stdio.h>
    void readerr(char *prompt,char *name_combined,int name_size);
    #define name_size 100
    struct firstname
    {
    	char first[name_size];
    }typedef struct firstname emp;
    struct lastname
    {
    	char lastname1[name_size];
    }typedef struct lastname emp2;
    void readerr(char *prompt,char *name_combined,int name_size)
    {
    	fputs(prompt,stdout);
    	fgets(name_combined,name_size,stdin);
    	fflush(stdin);
    }
    int main(void)
    {
        char question[]="Please enter your first name: ";
        char question1[]="Please enter your last name: ";
    	emp * first2;
        emp2* lastname2;
        readerr(question,first2->first,name_size);
        readerr(question1,lastname2->lastname1,name_size);
        printf("Now full name is %s",first2->first->lastname2);
        return 0;
    }
    can someone explain like struct ptrs within struct ptrs
    thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Code:
    test.c:7: error: two or more data types in declaration of `emp'
    test.c:11: error: two or more data types in declaration of `emp2'
    test.c:12: error: syntax error before numeric constant
    test.c: In function `readerr':
    test.c:13: error: number of arguments doesn't match prototype
    test.c:2: error: prototype declaration
    test.c:14: error: `prompt' undeclared (first use in this function)
    test.c:14: error: (Each undeclared identifier is reported only once
    test.c:14: error: for each function it appears in.)
    test.c:15: error: `name_combined' undeclared (first use in this function)
    test.c: In function `main':
    test.c:26: error: request for member `lastname2' in something not a structure or union
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Do you mean something like this:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct info {
    	char data[256];
    	int ID;
    };
    
    struct wrapper {
    	char source[64];
    	char dest[64];
    	struct info content;
    };
    
    void dostruct (struct wrapper* here) {
    	printf("DATA: %s\nID: %d\n",here->content.data,here->content.ID);
    }
    
    int main () {
    	struct wrapper example;
    	strcpy(example.content.data,"hello world");
    	example.content.ID=667;
    	dostruct(&example);
    	return 0;
    }
    Beware the mixed notation in dostruct(): here->content.data

    If struct wrapper used a struct info*, I would have had to allocate it and then in dostruct() there would be the more normative "here->content->data". That is more normative because it's more useful to use a pointer in a wrapper like this. Another useful method is to use an int member "length" in struct info to indicate the length of the data inside, especially if "data" were a char* into a block which is not null terminated -- as is sometimes the case with network packets.
    Last edited by MK27; 02-23-2009 at 11:17 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    100+ posts, and still using fflush(stdin).
    This is going to take a while
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    oh yah mixed notation after that . yes i got it now well for fflush(stdin); is just a hobby but before program done i do getchar(); but sometimes problems appear espicially for nums i dunno how to avoid that

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You avoid it by NOT mixing input styles within your program. Chaotic choices of gets(), scanf(), getchar() etc result in a mess of the input stream.

    Use fgets() EVERY time, then use sscanf() or anything else you like to examine the line of input in memory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault with Linked List Program
    By kbrandt in forum C Programming
    Replies: 1
    Last Post: 06-23-2009, 07:13 AM
  2. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  5. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM