Help with structs.

This is a discussion on Help with structs. within the C Programming forums, part of the General Programming Boards category; Hi, I have a problem with sturcts. I have something like : Code: struct stack1 { int number; struct stack1 ...

  1. #1
    Registered User
    Join Date
    Jan 2011

    Help with structs.

    I have a problem with sturcts.
    I have something like :
    struct stack1 
       int number;
       struct stack1 *next;
    I want to create structs as user request.
    I mean, i`ll get some number from the user input, and create this number of startcts.
    For example, i got the number 3 from the user.
    Automaticly create :
    stack1 s1; stack1 s2; stack1 s3; ( 3 structs ).
    I dont care the structs names.
    I hope the explanation was clear, Thanks for helpers.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Create a dynamic array of struct stack1.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2011
    Around 8.3 light-minutes from the Sun
    Not really. Are you trying to create a linked list of structures? If that is the case take a look at this tutorial. If you are just trying to dynamically allocate an array of structures you would need something like this:
    #include <stdio.h>
    #include <stdlib.h>
    struct myStruct{
    	int i;
    int main(void){
    	struct myStruct *ptrStruct = malloc(2*sizeof(myStruct));	
    	ptrStruct[0].i = 3;
    	ptrStruct[1].i = 4;
    	for(int i=0;i<2;i++)
    		printf("%d\n", ptrStruct[i].i);
    EDIT: Of course you would check the return value of malloc to ensure you actually got the memory you requested.
    Last edited by AndrewHunter; 07-19-2011 at 11:48 AM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [ noob question ] Help with structs within structs
    By Riverfoot in forum C Programming
    Replies: 3
    Last Post: 04-26-2011, 07:24 PM
  2. Passing Structs Into An Array Of Structs.
    By TheTaoOfBill in forum C Programming
    Replies: 3
    Last Post: 10-07-2010, 09:38 AM
  3. Replies: 3
    Last Post: 03-31-2009, 12:34 PM
  4. passing structs & pointers to structs as arguments
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 03-16-2004, 06:14 PM
  5. Help with structs
    By Kendrose in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2002, 09:46 AM

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