Click here to Skip to main content
15,889,651 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
SuggestionRe: How to use c + + and web interaction Pin
Richard MacCutchan21-May-14 21:54
mveRichard MacCutchan21-May-14 21:54 
QuestionC program for Embedded controller ICP CON I7188XAD Pin
Member 1083123320-May-14 0:03
Member 1083123320-May-14 0:03 
AnswerRe: C program for Embedded controller ICP CON I7188XAD Pin
CPallini20-May-14 0:47
mveCPallini20-May-14 0:47 
AnswerRe: C program for Embedded controller ICP CON I7188XAD Pin
leon de boer20-May-14 3:22
leon de boer20-May-14 3:22 
GeneralRe: C program for Embedded controller ICP CON I7188XAD Pin
Randor 20-May-14 13:00
professional Randor 20-May-14 13:00 
Question[C]Strings' array goes crazy. Pin
militandri19-May-14 1:29
militandri19-May-14 1:29 
AnswerRe: [C]Strings' array goes crazy. Pin
Freak3019-May-14 2:04
Freak3019-May-14 2:04 
AnswerRe: [C]Strings' array goes crazy. Pin
enhzflep19-May-14 2:16
enhzflep19-May-14 2:16 
I think you'll find that the line:
C++
filenames[i]=dird->d_name;
should actually be something like:
C++
filenames[i]=strdup(dird->d_name);


You see, dird->d_name is a location in memory that the current file's name is copied to - _but_ this location doesn't change, only the contents of the text does. What your code does is set each of the filename[] elements to hold this location. In the next iteration of the loop, the text contained there changes, but the location doesn't. So you've got all elements of filename[] holding the same address of a string.

If, on the other-hand, you make a duplicate of the string in dird->d_name and then store _that_ location into filenames[], each element of filenames[] will point to a different location in memory - locations that contain the string you're after.

Note - you should also change your last code-block.
C++
filenames = malloc(n_files * sizeof(char*) );


So,
1) filenames should be a char** (I expect you've got it setup to be a char* currently)
2) each element (a char*) should contain the address of the duplicated string
3) No need for the filenames[i] = malloc(20 * sizeof(char) ); code.
GeneralRe: [C]Strings' array goes crazy. Pin
militandri19-May-14 2:57
militandri19-May-14 2:57 
GeneralRe: [C]Strings' array goes crazy. Pin
enhzflep19-May-14 3:06
enhzflep19-May-14 3:06 
Question[IFileIsInUse] : Need help with its implementation Pin
Mohsin Munawar18-May-14 18:23
Mohsin Munawar18-May-14 18:23 
AnswerRe: [IFileIsInUse] : Need help with its implementation Pin
Richard MacCutchan18-May-14 21:33
mveRichard MacCutchan18-May-14 21:33 
QuestionCreate style element with c++ Pin
vutrulavotann17-May-14 1:11
vutrulavotann17-May-14 1:11 
QuestionLZMA SDK and .zip files Pin
John Malcosky16-May-14 4:25
John Malcosky16-May-14 4:25 
SuggestionRe: LZMA SDK and .zip files Pin
Code-o-mat16-May-14 6:26
Code-o-mat16-May-14 6:26 
GeneralRe: LZMA SDK and .zip files Pin
John Malcosky16-May-14 8:13
John Malcosky16-May-14 8:13 
AnswerRe: LZMA SDK and .zip files Pin
Graham Breach16-May-14 8:46
Graham Breach16-May-14 8:46 
GeneralRe: LZMA SDK and .zip files Pin
John Malcosky16-May-14 9:24
John Malcosky16-May-14 9:24 
AnswerRe: LZMA SDK and .zip files Pin
leon de boer16-May-14 19:00
leon de boer16-May-14 19:00 
QuestionDouble buffering Pin
imran.prdc14-May-14 20:58
imran.prdc14-May-14 20:58 
QuestionRe: Double buffering Pin
CPallini14-May-14 21:39
mveCPallini14-May-14 21:39 
AnswerRe: Double buffering Pin
Legor14-May-14 21:54
Legor14-May-14 21:54 
AnswerRe: Double buffering Pin
Richard MacCutchan14-May-14 21:56
mveRichard MacCutchan14-May-14 21:56 
AnswerRe: Double buffering Pin
Software_Developer15-May-14 4:35
Software_Developer15-May-14 4:35 
AnswerRe: Double buffering Pin
Stefan_Lang15-May-14 22:21
Stefan_Lang15-May-14 22:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.