Click here to Skip to main content
15,903,201 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: How To Decrease Video Size Pin
AmbiguousName22-May-14 1:06
AmbiguousName22-May-14 1:06 
GeneralRe: How To Decrease Video Size Pin
SoMad22-May-14 15:12
professionalSoMad22-May-14 15:12 
AnswerEncode it as an MP4 File Pin
Software_Developer22-May-14 1:49
Software_Developer22-May-14 1:49 
QuestionI need a two level priority queue Pin
Member 1042711521-May-14 19:24
Member 1042711521-May-14 19:24 
AnswerRe: I need a two level priority queue Pin
«_Superman_»21-May-14 19:53
professional«_Superman_»21-May-14 19:53 
GeneralMessage Closed Pin
21-May-14 19:59
Member 1042711521-May-14 19:59 
GeneralRe: I need a two level priority queue Pin
«_Superman_»21-May-14 20:45
professional«_Superman_»21-May-14 20:45 
GeneralMessage Closed Pin
21-May-14 20:49
Member 1042711521-May-14 20:49 
GeneralRe: I need a two level priority queue Pin
«_Superman_»21-May-14 21:33
professional«_Superman_»21-May-14 21:33 
GeneralRe: I need a two level priority queue Pin
Member 1042711521-May-14 21:49
Member 1042711521-May-14 21:49 
GeneralRe: I need a two level priority queue Pin
Member 1042711521-May-14 21:54
Member 1042711521-May-14 21:54 
QuestionHow to use c + + and web interaction Pin
jingjing yuan21-May-14 3:20
jingjing yuan21-May-14 3:20 
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 

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.