Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9303049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:23:41+00:00 2026-06-18T23:23:41+00:00

So I want to have an array whose size is not known initially (to

  • 0

So I want to have an array whose size is not known initially (to be taken from the user from command line) but I want to have it globally accessible(outside main).

So here is what I do:

 //.h file declaration
 typedef struct res
 {
   int popultaion[NB_TYPE];
   int alive;
   int birthat[NB_TYPE];
 }res_t;

 //.c file
 res_t* res_first = NULL;

 int main(int argc, char* argv[])
 {
    int no_of_mutants = atoi(argv[1]);
    int i,j = 0;
    srand(time()NULL);
    res_t* tem= res_first;
    for(i = 0; i < no_of_mutants; i++)
    {      

       for(j = 0; i < NB_TYPE; i++)
       {             
         tem->popultaion[j] = rand();
       }
       tem++
    }
    //...other code
  }

I get a segmentation fault., When I debug using gdb, it seems like it is here it gets the SIGSEGV.

      tem->popultaion[j] = rand();

I have two questions:

  1. Is this form of trying to construct an array using adding pointer valid? I am getting segfault in the first run though when the address should technically be valid.
  2. Is the way of accessing a an array member from a struct valid? (Kind of sure it is but just want to make sure)?

P.S. I am aware of malloc and just could achieve the same thing using malloc but I want to know if this is what giving me problem before going to change everywhere else in the code and if so why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T23:23:42+00:00Added an answer on June 18, 2026 at 11:23 pm

    Is this form of trying to construct an array using adding pointer valid? I am getting segfault in the first run though when the address should technically be valid.

    Why should it? It’s NULL, so your code invokes undefined behavior. What you have to do is allocate memory for it when the user entered the number of items in the array.

    res_first = malloc(sizeof(*res_first) * NUM_OF_ELEMENTS);
    

    and of course, free it after use:

    free(res_first);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a multidimensional array whose index/keys (not the values) are like this: this
I want to have an array of ArrayLists: ArrayList<MyClass>[] myArray; I want to initialize
What if i want to have an array of pointers to a function and
I have a NumPy array [1,2,3,4,5,6,7,8,9,10,11,12,13,14] and want to have an array structured like
VISUAL C++ Question Hi, I have array of 3 elements and I want to
i have an array and i want to convert this array in comma seprated
I have an array which I want to use in a query: Array (
I have an array I want to check the the last digits if it
I have to array and I want to merge this two array with a
I have an array that I want to sort based on the value of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.