0

Basically I have two structs and I want to make an array of A structs, and within each A struct I want an array of 50 B structs. So I assume that we will use double pointers.

struct A{
    char* a_word;
    struct B** b_list;
};

struct B{
   char* b_word;
   int b_value;
};

When call initialize function I initialize the structs like this. My goal is to set all the values to NULL when I allocate memory.

struct Word** initialize()
{
    int k;
    int i; 
    struct A** A_list = calloc(BUFFSIZE, sizeof(struct A*));
    for(k =0; k < BUFFSIZE; k++)
    {
     A_list[k] =  calloc (1, sizeof(struct A)); 
     A_list[k]-> b_list =  calloc(50, sizeof(struct B*));

        for(i = 0; i < 50; i++)
        {
           A_list[k]->b_list[i] =  calloc(1, sizeof(struct B)); 
        }

      }
return hashTable;
} 

after initializing all these values I am able to do. . .

if(A[43]->a_word == NULL) //43 unallocated value
  {
    printf("This is null\n");
    //program prints this statement - good
    //program knows that this value is NULL
  }

But I also want . .

if(A_list[44]->b_list[0] == NULL)
{
   printf("This is also null");
   //This should be printed but nothing happens
}

For some reason not matter if I set the above if statement to == NULL or != NULL the program outputs absolutely nothing from that if statement. What is going on in memory and how can I allocate everything correctly, and so the value is set to NULL as a default and so I can input a value?

EDIT: Also whenever try to do A_list[value1]->b_list[value2] = strdup("string"); I get a segmentation error, this most likely stems from the same problem.

4
  • 1
    What do you think this is doing: A_list[k]->b_list[i] = calloc(1, sizeof(struct B)); ? Hint: it is not assigning NULL to that pointer. So testing for one later isn't going to do you much good. Now, what problem is this model supposed to be solving, a two-level hash table? Commented Apr 26, 2014 at 7:00
  • Yes I am making a two level hash table. In that calloc statement I am assuming that I am allocating space for a B struct. Commented Apr 26, 2014 at 7:02
  • ...which means checking A_list[44]->b_list[0] == NULL will evaluate as false, since you're initialization loop clearly populates it with an allocation address (assuming calloc doesn't fail, which should also be checked). Testing for non-null and fixing the output flush as alk's answer shows should get you output. Commented Apr 26, 2014 at 7:06
  • Your posted code won't compile. You don't have the declaration of hashTable. You should post the actual code that you are having problems with. Commented Apr 26, 2014 at 7:09

1 Answer 1

4

As mentioned already by WhozCraig in a comment to the question, this code

    for(i = 0; i < 50; i++)
    {
       A_list[k]->b_list[i] =  calloc(1, sizeof(struct B)); 

initialises the first 50 elements of b_list to point to valid memory, that is to be non 0, assuming calloc() never fails. Being that optimistic you better test for those elements being != NULL.

... if I set the above if statement to == NULL or != NULL the program outputs absolutely nothing

The code does not seem to flush stdout here:

if(A_list[44]->b_list[0] == NULL)  
{
   printf("This is also null");

Change this by adding a final \n:

if(A_list[44]->b_list[0] != NULL)  
{
   printf("This isn't null\n");

As stdout is line buffered by default, all content will be flushed if a new-line is detected.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.