954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

copying single array to 2D array

i got a little question hope you guys can help out

i wanna copy a char array[8] to a 2D array , strcpy doesn't let me do it. how should i go about

char letter[8]="abcdefg";
char temp[10][2];

for(int i=0;i<10;i++)
strcpy(temp[i],letter[i])

how do i get extract all the letters into the 2D array ?
so that
temp[0]=a
temp[1]=b
temp[2]=c
etc... thanks !

tropical08
Newbie Poster
2 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

copy it one letter at a time, like this and also the loop needs to stop when end-of-string null terminator is found in the letter array.

char letter[8]="abcdefg";
char temp[10][2] = {0};

for(int i=0; letter[i] != 0;i++)
   temp[i] = letter[i];
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

thanks but running the code would give incompatible assign types

temp[i][0] = letter[i]; solves it =)

tropical08
Newbie Poster
2 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You