C structure within structure problem, need help

This is a discussion on C structure within structure problem, need help within the C Programming forums, part of the General Programming Boards category; I have a structure and within that structure, i have a declaration of a structure of type different structure. So ...

  1. #1
    Unregistered
    Guest

    Question C structure within structure problem, need help

    I have a structure and within that structure, i have a declaration of a structure of type different structure. So im trying to store data through a function that only passed the artist type structure.

    struct cd {
    int cdnum;
    int cdcount;
    char cdtitle[80];
    struct cd *next;
    };

    struct artist
    char name[80];
    struct cd *link;
    };


    so in a function i later write, a structure of type artist is passed by pointer. I want to store data into the "cd structure" portion of the artist structure.

    this is what i thought would work:
    scanf("%d", p->link.cdnum);

    but it doesn't and im stumped. I still think it would work even though it wont compile . can someone help me out?

    heres the protype for that later function just incase:
    void newcd(struct artist *p,int size);

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > scanf("%d", p->link.cdnum);

    Is "p" a pointer? If so:

    &p->link->cdnum

    If not:

    &p.link->cdnum

    Remember, scanf uses addresses. So, if the final variable you're trying to use is not a pointer, you must provide its address.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Unregistered
    Guest
    Thanks for the quick reply!

    That didn't seem to work for me however :D (i do go now a little farther in the program before it crashes though : ) )

    This is that function i wrote:

    void newcd(struct artist *p, int size)

    {
    struct cd *tmp;
    tmp = p->link;

    printf("Artist name:\n");
    gets(p->name);
    puts (p->name);

    printf("CD number:\n");
    *scanf("%d", &tmp->cdnum);

    printf("CD title:\n");
    gets(p->link->cdtitle);

    printf("The ammount to be stocked:\n");
    scanf("%d", &p->link->cdcount);
    }

    My program now crashes right at the * i labeled. the printf line with "CD number" will show, and then i enter in a digit, hit return and it crashes.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > tmp = p->link;


    > gets(p->link->cdtitle);

    You never check to see if 'p->link' is NULL. Make sure you've allocated it, otherwise, this may be why your program tanks.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Unregistered
    Guest
    Same unregistered user as before, different question.

    with the segment of code i have below, im trying to manipulate a structure with [100] elements to it.

    int size=100;
    struct artist artist_array[size];

    how can i manipulate a different index inside a function that had this array passed through it? p[i]->name is not a valid way to use pointers is my guess.
    Insight apreciated : ). and if anyone knows any good sites with pointers, that as well would be apreciated :))

    void details(struct artist *p, int size)


    {
    char name[80];

    int i = 0;
    printf("Enter the artist name:\n");
    gets(name);

    while( i < 100)
    {
    if (name == p[i]->name)
    { printf("Artist: %s\n", p[i]->name);
    printf("CD title: %s\n", p[i]->link->cdtitle);
    printf("Stocked: %d\n", p[i]->link->cdcount);}
    else i++;}
    }

  6. #6
    Unregistered
    Guest
    Try this:
    Code:
    void details(struct artist *p, int size){
    	char name[80];
    	int i = 0;
    
    	printf("Enter the artist name:\n");
    	fgets(name, sizeof(name), stdin);
    
    	while( i < 100){
    		if (strcmp(p[i].name, name) == NULL){ 
    			printf("Artist: %s\n", p[i].name);
    			printf("CD title: %s\n", p[i].link.cdtitle);
    			printf("Stocked: %d\n", p[i].link.cdcount);
    			break;
    		}else{ 
    			i++;
    		}
    	}
    }
    p is an array of structures, so you access it like an array but you access it's members as if it were just a single structure, ie. with the dot operator

    You also want to exit the loop upon finding what you were searching for to save processing time.

    -Prelude

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem getting structure to work in my function
    By Tom Bombadil in forum C Programming
    Replies: 18
    Last Post: 05-28-2009, 10:53 AM
  2. Problem with structure and class
    By Bargi in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2007, 03:30 AM
  3. Problem with arrays inside a structure
    By babu in forum C Programming
    Replies: 4
    Last Post: 07-12-2007, 10:35 AM
  4. accessing structure pointer problem
    By godhand in forum C Programming
    Replies: 2
    Last Post: 04-09-2004, 11:52 PM
  5. Problem checking for numeric value in a structure
    By ronkane in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2002, 02:53 PM

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