Array of struct

This is a discussion on Array of struct within the C Programming forums, part of the General Programming Boards category; Ok, this is my problem I have a structure : Code: struct store{ int data_int; char data_char; int num; }; ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    29

    struct passing

    Ok, this is my problem

    I have a structure :

    Code:
    struct store{
    	int data_int;
    	char data_char;
    	int num;
    };
    I then use this function :

    Code:
    struct store peek(stack_t **stackptr, int size)				
    
    {
    	stack_t* peek_element;						
    
    	stack_t* store_element;						
    	int i;	
    	int d_int;
    	char d_char;
    	store_element = (*stackptr);					
    
    	printf("Peeking at stack:: ");	
    	struct store peeking;
    			
    
    	for ( i=0; i < size; i++)					
    
    	{
    
    		peek_element = (*stackptr);       				
    
          		peeking.data_int = peek_element->data_int;
    		peeking.data_char = peek_element->data_char; 	    			
    		(*stackptr) = peek_element->next;
    								
    
    	}
    	return peeking;	//Returns  value at top of stack
    
    	printf("\n");							
    
    	(*stackptr) = store_element;
    Calling of the function ::

    test is an array of the struct above.

    Code:
    	for (i=0; i > x ; i++) 
    	{						 
    		test[i] = peek(&stack,i);
    		printf("\n%d", test[i].data_int);
    		printf("\nc", test[i].data_char);
    	
    	}
    I get a segmentation error...I'm not sure what that means, and I don't understand why I am getting an error, I would think it would work properly....hmmmm. Any ideas?
    Last edited by zonf; 12-18-2005 at 08:42 AM.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    right what is this
    Code:
    (*stackptr) = peek_element->next;
    your structure don't seem to have a data member li9ke next. i hope u aree trying to do something like linked list struct. id you want to include your next as a member to your structure this how u do it

    Code:
    struct store{
    	int data_int;
    	char data_char;
    	int num;
                    struct store *next;
    };
    as u dont have a link between the array elemets u cant access any of your data. can i look at your main fucntion please

    ssharish

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    29
    Code:
    struct element{
    	int data_int;
    	char data_char;
    	struct element* next;
    }
    stack_t;
    This structure is used for pushing data onto the stack..., the whole idea of my peek function, is that I get all the data off the stack without deleting any of it.

    Main function ::

    Code:
    int main(int argv, char*argc[])
    {
    
    	int a,b,c,d,i,k;
    	char q,r,s,t;
    
    	printf("Enter any 4 integers and 4 chars seperated by spaces ");
    
    	scanf("%d %d %d %d %c %c %c %c", &a,&b,&c,&d,&q,&r,&s,&t);			          //Read in user 				
    
    
    
    	push(a,q,&stack);							  
    
    	push(b,r,&stack);
    
    	push(c,s,&stack);
    
    	push(d,t,&stack);
    
    
    	
    	for (i=0; i > 4 ; i++) 
    	{						 
    		test[i] = peek(&stack,i);
    	}
    }
    Last edited by zonf; 12-18-2005 at 09:18 AM.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    i dont understand what are u doing here. it seems to to be meaning less. how do u travel through the stack if u dont have a link to next element. your struture looks like it dont have any link to next record
    Code:
    struct store{
    	int data_int;
    	char data_char;
    	int num;
    };
    are u using any array here if so can u post here

    ssharish2005

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    29
    I have a stack.c and a stack.h file that I include with my test.c file.

    stack.h file contains the structure

    Code:
    struct element{
    	int data_int;
    	char data_char;
    	struct element* next;
    }
    stack_t;
    My stack works correctly....within the for loop I have created in my function, it travels through the stack looking at each element.

    I then use this structure to return these values....

    Code:
    struct store{
    	int data_int;
    	char data_char;
    };
    Are you saying that I should just return the structure 'struct element' instead?

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    i dont see any array declaration or stack pointer either in your main. right listen how do u manupalate on stack u need a stack pointer which points to the top of the stack. i dont see a stack pointer.

    ssharish2005

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by zonf
    I have a stack.c and a stack.h file that I include with my test.c file.

    stack.h file contains the structure

    Code:
    struct element{
    	int data_int;
    	char data_char;
    	struct element* next;
    }
    stack_t;
    My stack works correctly....within the for loop I have created in my function, it travels through the stack looking at each element.

    I then use this structure to return these values....

    Code:
    struct store{
    	int data_int;
    	char data_char;
    };
    Are you saying that I should just return the structure 'struct element' instead?
    right if this is the case
    Code:
    for ( i=0; i < size; i++)					
    
    	{
    
    		peek_element = (*stackptr);       				
    
          		peeking.data_int = peek_element->data_int;
    		peeking.data_char = peek_element->data_char; 	    			
    		(*stackptr) = peek_element->next;
    								
    
    	}
    	return peeking;	//Returns  value at top of stack
    so in the main u will have to recive the peeking. so it should me like this
    Code:
    strut store temp;
    
    temp=(&stackpointer,size);
    
    printf("%d",temp.data_int);
    printf("%c",temp.data_char);

    ssahrish2005

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    29
    I'm not using a pointer to point to the top of the stack...do I need to?

    If I enter in

    1 2 3 4 a b c d

    and use the following code

    Code:
    struct store{
    	int data_int;
    	char data_char;
    	int num;
    } test2;
    
    ...
    
     test2 = peek(&stack,3);
    The output would be:

    Peeking at stack:: 2b

    This means my stack is working....



    If however I use this array:

    (sorry, the array definition didn't get copied when I last posted...am really tired, so please bear with me as best you can, sorry for being a pain)

    Code:
    struct store{
    	int data_int;
    	char data_char;
    	int num;
    } test[4];
    
    ...
    for (i=0, i<4; i++)
    {
         test[i] = peek(&stack,i);
    }
    I get a sgementation fault....


    Code:
    #include<stdio.h>
    
    #include"stack.h"
    
    struct store{
    	int data_int;
    	char data_char;
    };
    
    stack_t* stack;  		
    int stack_size;
    ....

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    29
    Yes you are exactly right in your post above....It doesn't work when I try and recieve it into an array though...

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    Post your latest code - a main() and enough functions to show your segfault.

  11. #11
    Registered User
    Join Date
    Dec 2005
    Posts
    29
    Don't worry, all my problems solved now!
    Last edited by zonf; 12-18-2005 at 11:05 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 02:10 AM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 02:55 AM
  3. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  4. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 10:50 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM

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