0

I have 2 structs in my program.

linked list of id's, and a WORD

typedef struct related
{
 int id;
 struct related* next;
} RELATED;
typedef struct word
{
 int id;
 char name[NAME_LEN];
 RELATED *related;
} WORD;

i want to hold an array of WORD's and i want it to be dynamic.

I have 2 pointers to words:

WORD* word1;
WORD* word2;

with values.

when i am tryin to dynamically allocate it in this way:

WORD** arr = (WORD**)malloc(sizeof(WORD*)*10) // to hold 10 words

and trying to add a word to an array, the first one is added properly, but the second one run over the first one:

arr[0] = word1;
arr[1] = word2;

when i am defining the array this way:

WORD* arr[40];

the same add of words works just fine

arr[0] = word1;
arr[1] = word2;

can't quite find the right way of this dynamic allocation.. thnx for help!

asked Jan 9, 2014 at 23:58
2
  • Seems to work fine for me? (Yes, not free'ing memory, fine for this purpose though) Commented Jan 10, 2014 at 0:46
  • It should work fine, can you share your program code? Commented Jan 10, 2014 at 4:35

3 Answers 3

1

Rather than

// arr as pointer to pointer to WORDS
WORDS** arr = (WORDS**)malloc(sizeof(WORDS*)*10);

Allocate

// arr as pointer to WORDS
WORDS* arr = malloc(sizeof(*arr) * 10); // WORDS *, not WORDS **
  1. C gibberish ↔ English may be useful.
  2. Casting the result of malloc() is discouraged in C.
  3. Consider the type *p = malloc(sizeof(*p) * N); style. Easier to maintain and less error prone IMO.
answered Jan 10, 2014 at 0:01
Sign up to request clarification or add additional context in comments.

2 Comments

What is not clear is the comment "to hold 10 words". Your malloc() holds 10 pointers to WORDS. Further, Are you simple wanting to copy pointers word1, word2 or the date they point to?
@Andrey Chasovski How did you determine "but the second one run over the first"? With your edited info, it appears you are doing things right - need more info.
1

I couldn't find any issue with malloc() and using pointer to pointer of WORD as array. One thing I can point out that, you have not allocated memory for word1 and word2 they hold a garbage address. Same garbage address is being assigned to arr[0] and arr[1]. If you upload complete code, it will help in finding out exact problem.

answered Jan 10, 2014 at 4:41

Comments

0

You can use following code snippet for dynamic memory allocation for 2D array :

Let 'm' and 'n' are dimensions of an array .

WORDS** arr = (WORDS**)malloc(sizeof(WORDS*)*m);
for(int i = 0 ; i < m ; i++)
{
 for(int j = 0 ; j < n ; j++)
 {
 arr[i] = (WORDS*)malloc(sizeof(WORDS)*n);
 }
}
answered May 22, 2014 at 11:52

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.