Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have an SD card which I filled with 15 mp3 tracks and a text file that has 15 words and 15 tracks names. My question is regarding initializing the following

char* words[ ]
char* tracks [ ]

When I initialize them as [100] and [100], the listing of the dB doesn't work as I currently only have 15 tracks and words. I want code to have the ability to place 999.

So my question is: can I make both them variable arrays and how?

I tried leaving between the brackets empty and several other methods but they didn't work. So I wondering if anyone here have any other suggestions.

share|improve this question
    
Use dynamic memory allocations, malloc and then realloc? – Alok Save Dec 29 '13 at 15:44
    
I tried using malloc but it didn't work, however will try to use realloc, thanks for your fast reply :D – user3133400 Dec 29 '13 at 15:50
    
Note char*words[] is an array of pointers! You will need buffer for string data also char[][]. As this is all dynamic, you better not use static arrays, but use some new keywords. – LS_dev Dec 30 '13 at 10:31

Arduino code is compiled as C++; you can use the new char[size] and delete[] operators.

share|improve this answer
    
I tried what still i didnt get anything but when i tried using: //words = (char** )calloc(track_size++,sizeof(char*)); //tracks = (char** )calloc(track_size++,sizeof(char*)); with delete []; it only outputs the last word and last track, i dont understand why?? – user3133400 Dec 29 '13 at 20:12

Arduino version of C/C++ has a great feature - VLA (variable length array, from C99) So, just use:

void process(int n)
{
    // Set up a buffer of n characters
    char b[n];
    // do the work
}

See: http://www.drdobbs.com/the-new-cwhy-variable-length-arrays/184401444

You need array of arrays? No problem (I hope). Pseudocode:

char* words[N]; // N - words
char word1[M] // M - word length
char word2[O] // O - second word length

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

Many answers suggest to use dynamic memory allocation - it works, but main drawback of such method - memory fragmentation. But remember - dynamic memory allocation is only method to return arrays from functions!

P.S. I can't verify code with compiler at the moment - please give me feedback if it doesn't work.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.