1
#include <stdio.h>
#include <stdlib.h>

typedef struct nodeWords
{
    char * word;
    int    index;
    struct nodeWords *left;
    struct nodeWords *right;
} nodeWords;

int main(void)
{

    nodeWords * node = malloc(sizeof(*node));
    printf("%lu\n",sizeof(*node));
    node->left = NULL;
    node->right = NULL;

    nodeWords  * ihash = malloc(2 * sizeof(*ihash));        
    printf("%p \n", node->left); 

    //this part not working   
    ihash[0] = *node->left;
    printf("%p\n",ihash[0]);

}

How can I assign node->left to ihash[0] and then be able to print out ihash[0], which should point to NULL?

2
  • 2
    It doesn't make sense. ihash[0] is a struct. There's no such thing as a NULL struct. Not entirely sure what you are trying to do. Perhaps you want ihash to be an array of struct pointers instead? nodeWords **ihash = malloc(2 * sizeof(*ihash)); ihash[0] = node->left;? Commented Sep 21, 2020 at 20:59
  • So ihash[0] should itself be a pointer, right? In your code, it's the struct nodeWords. Commented Sep 21, 2020 at 20:59

1 Answer 1

1

There are two errors in your code and a few other 'minor issues' (I've commented these in the code posted below).

The first error is that you want to create an array of pointers to nodeWords, so you will need two stars in the declaration of ihash (one star will create an array of structure objects).

Second, in ihash[0] = *node->left;, you are dereferencing node twice (once with the preceding star operator, and once again with the -> operator.

The following code fixes these issues:

#include <stdio.h>
#include <stdlib.h>

typedef struct nodeWords {
    char* word;
    int    index;
    struct nodeWords* left;
    struct nodeWords* right;
} nodeWords;

int main(void)
{

    nodeWords* node = malloc(sizeof(*node));
    printf("%zu\n", sizeof(*node)); // Should really use "%zu" for size_t 
    node->left = NULL;
    node->right = NULL;

    nodeWords** ihash = malloc(2 * sizeof(*ihash)); // You want an array of POINTERS so you need two ** in the type!
    printf("%p \n", (void*)node->left); // Pedantic: %p expects a void*

    //this part not working   
    ihash[0] = node->left; // The "*" preceding "node" was an error: the "->" inherentlt derefernces node
//  ihash[0] = (*node).left; // An alternative way of dong the same thing
    printf("%p\n", (void*)ihash[0]); // Pedantic: %p expects a void*

    // Don't forget to free the allocated memory...
    free(ihash);
    free(node);

    return 0; // Always good practice to put this EXPLICIT return statement in your "main"
}
Sign up to request clarification or add additional context in comments.

5 Comments

does nodeWords** mean a pointer to an array of pointers or is there some other way I should think about the double **
@DCR If you have something like int* ptr = malloc(5 * sizeof(int)); then ptr will be a pointer to a block of memory that can be treated like an array of int objects. Adding the extra star, like int** ptr2 = malloc(5 * sizeof(int*)); makes ptr2 an 'array' of int* objects - or an 'array' of pointers-to-ints. It is similar in your case. (Does this make sense?)
@DCR Maybe it is clearer writing with different spacing, like: nodeWords* *ihash = malloc... ?
so with one * and malloc we get a block of memory that can be treated like an array and would hold objects of some type and with two * we get a block of memory that will hold an array of pointers - and each of those pointers point to a block of memory that holds some type of object. Does that make sense?
@DCR Almost! But and each of those pointers point to a block of memory that holds some type of object will only be true once you have explicitly set each to point to valid memory.

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.