Jump to content
  • Advertisement
Sign in to follow this  
Crypter

Odd strcpy problem

This topic is 3665 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey everyone, Im having some trouble with a piece of code. Basically I am trying to use strcpy() to set a single entry in an array of strings. However, instead of setting one entry, it seems to set all entries to the same string. :/ Here is where the array is created. The software runs in an environment without dynamic allocation, so its created statically:
//! array of strings (max ENTRY_LENGTH characters per string)
static char _entries[MAX_ENTRIES][ENTRY_LENGTH];
m_bootEntries = (char**) _entries;

//! this option is always the first one
m_bootEntries[0] = "Enter Command Parser";
The first entry (shown above) is set correctly and works fine. The problem comes when I use strcpy() to copy a string into m_bootEntries. If I use strcpy(), it sets all corresponding entries to the same string rather then the one. :/
//! sets entries 1 to 'n' to this string??
strcpy (m_bootEntries[1], "entry string");
Does anyone know where I might of gone wrong? Thanks for any suggestions[smile]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Your cast is suspicious. Surely if such a conversion was legal the compiler wouldn't require a cast?

m_bootEntries is declared as a char**. Without the cast I get the following on that line of code:
error C2440: '=' : cannot convert from 'char [20][40]' to 'char **'
Are these not the same basic types? Or am I mistaken? (Probably...I did not like the cast either).

Share this post


Link to post
Share on other sites
Quote:
Original post by Crypter
cannot convert from 'char [20][40]' to 'char **'
Are these not the same basic types?

No. A two-dimensional array is very different from an array of pointers.

Share this post


Link to post
Share on other sites
No, arrays and pointers are not the same (looking for a good link).

This code seems to work, but its a bit ugly:

typedef char EntryList[MAX_ENTRIES][ENTRY_LENGTH];

static EntryList _entries;

void foo()
{
for(int i = 0 ; i < MAX_ENTRIES; ++i)
{
_entries[0] = '\0';
}

EntryList *m_bootEntries = &_entries;

strcpy((*m_bootEntries)[0],"Enter Command Parser");

strcpy ((*m_bootEntries)[1], "entry string");
}

Share this post


Link to post
Share on other sites
Why so complicated?

static char _entries[20][40];
strcpy(_entries[0], "Enter Command Parser");
strcpy(_entries[1], "entry string");

Share this post


Link to post
Share on other sites
With the m_ prefix, I assumed he wanted to reference the array indirectly, as a member of some structure. If you have direct access to the entries table, then yes of course that would be easier.

Share this post


Link to post
Share on other sites
Alright then, pointers to arrays to the rescue:

#define MAX_ENTRIES 20
#define ENTRY_LENGTH 40

static char _entries[MAX_ENTRIES][ENTRY_LENGTH];
char (*m_bootEntries)[ENTRY_LENGTH] = _entries;
// the 2D array decays into a pointer to an array of 40 characters

strcpy(m_bootEntries[0], "Enter Command Parser");
strcpy(m_bootEntries[1], "entry string");

Share this post


Link to post
Share on other sites
I was trying to get something similar to compile... but I must have left something out, it wouldn't compile. Either that or I mismatched the array sizes or something equally stupid [embarrass].

Share this post


Link to post
Share on other sites
Quote:
With the m_ prefix, I assumed he wanted to reference the array indirectly, as a member of some structure. If you have direct access to the entries table, then yes of course that would be easier.

That was indeed my intention. I wanted to indirectly refrence the array through a member pointer to try keeping the size of the overall class down. I modified my code to work similarly to what you have posted and it worked!

DevFred's solution seems like it should work as well. Im going to try DevFred's solution as well as it seems easier to work with.

Thanks for the help! [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!