storing values of 3d array

Hi there,
I need help with a part of my program.
Im trying to store certain values of a 3d array in another variable.

EX:

1
2
3
4
5
6
7
char buffer[20];                            //to store
char abc[3][10][10]={{"hello", "thEre", "hey"},{"now","later"},{"yes","no"}};  

strcpy(buffer,abc[0][1][2]); //need this to work

i want it to store the 'E' in buffer.


Please help.Thanks in advance.
closed account (1wvX92yv)
try the following code fragment instead.........


char buffer[20]; //to store
char abc[3][10][10]={{"hello", "thEre", "hey"},{"now","later"},{"yes","no"}};

cout<<abc[0][1][2];

cout<<"\n\n\n\n";

buffer[0]=abc[0][1][2];
cout<<buffer;

strcpy() takes two char* as arguments, abc[0][1][2] is a character.

&abc[1][2][3] is the address of 'E', but then strcpy() will make buffer: 'E', 'r', 'e', '\0'. You could put the null terminator at position 1 yourself and be all set.

You can also use strncpy, which takes pointers, but which also takes an amount as an argument:
http://www.cplusplus.com/reference/cstring/strncpy/

I believe this would work
1
2
3
int length = 1;
strncpy(buffer, &(abc[1][2][3]), length);
buffer[length] = '\0';  // strncpy does not promise a null terminator 


Last edited on
awesome! thanks a lot :)
Topic archived. No new replies allowed.