Thread: Initializing an array inside of a structure

  1. #1
    Registered User
    Join Date
    Mar 2007
    Location
    Portugal
    Posts
    18

    Initializing an array inside of a structure

    Hello,

    I have been tried to do this but I keep getting segmentation faults.

    Code:
    typedef struct
    {
       int *v;
       int x;
    } st;
    If I declare on main() a pointer to st, like st *s, how can I acess both variables in st?

    To access the regular int, I did it this way: s->x=...

    How to declare v with n elements, using malloc?

    Thank you.
    Last edited by Lima; 06-06-2009 at 06:20 PM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You have to allocate memory for the struct before you allocate memory for v.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    st s;
    s.n = 100;
    s.v = malloc(sizeof(your_type) * s.n);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    9
    but this declaration :
    s.v = malloc(sizeof(your_type) * s.n);
    malloc fn. return a void pointer so u have to make a cast type by writing
    s.v=(int *)malloc(sizeof(your_type) * s.n);

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not in C. In C++ you do, but in C++, you use new instead of malloc.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mostafa faisal
    malloc fn. return a void pointer so u have to make a cast type by writing
    s.v=(int *)malloc(sizeof(your_type) * s.n);
    Pointers to void are implicitly convertible to pointers to object types, hence the cast is unnecessary.
    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

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If your compiler complains about the lack of cast for malloc(), either one of the following issues have occured:

    1. You forgot to #include <stdlib.h>
    2. Your compiler is compiling as C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with arrays inside a structure
    By babu in forum C Programming
    Replies: 4
    Last Post: 07-12-2007, 09:35 AM
  2. Realloc problems with sturcture array inside structure
    By daveyand in forum C Programming
    Replies: 2
    Last Post: 03-29-2004, 06:48 AM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM