# Odd strcpy problem

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

## 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 on other sites
Your cast is suspicious. Surely if such a conversion was legal the compiler wouldn't require a cast?

##### Share on other sites
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 on other sites
Quote:
 Original post by Cryptercannot 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 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 on other sites
Why so complicated?
static char _entries[20][40];strcpy(_entries[0], "Enter Command Parser");strcpy(_entries[1], "entry string");

##### 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 on other sites
Alright then, pointers to arrays to the rescue:

#define MAX_ENTRIES 20#define ENTRY_LENGTH 40static char _entries[MAX_ENTRIES][ENTRY_LENGTH];char (*m_bootEntries)[ENTRY_LENGTH] = _entries;// the 2D array decays into a pointer to an array of 40 charactersstrcpy(m_bootEntries[0], "Enter Command Parser");strcpy(m_bootEntries[1], "entry string");

##### 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 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]

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 103
• 11
• ### Forum Statistics

• Total Topics
632976
• Total Posts
3009672
• ### Who's Online (See full list)

There are no registered users currently online

×