4 Replies - 3649 Views - Last Post: 17 March 2011 - 03:25 PM Rate Topic: -----

#1 musiliu  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 110
  • Joined: 04-December 09

C Question about Initializing 2D Char Array

Posted 17 March 2011 - 02:23 PM

if i have a 2d array like this:

char test2darray[10000][30];
test2darray[1] = "this is a test";  // does not work
strcpy(test2darray[1], "this is a test");  // works



can someone explain why the strcpy works but not the line before it?
Is This A Good Question/Topic? 0
  • +

Replies To: C Question about Initializing 2D Char Array

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: C Question about Initializing 2D Char Array

Posted 17 March 2011 - 02:54 PM

The short answer is that you can't just assign an array.

edit: the longer answer is that to copy "this is a test" into a char array, you have to copy each char individually into an element of the array.
When you try to assign "this is a test" to test2darray[1], you are actually trying to store a pointer to the string literal "this is a test" into the memory location (test2darray + 30) which you have already declared as type char.

To clarify further:
the array test2darray[1000][30] is just a contiguous region of 30000 bytes of memory which you have declared will hold 30000 chars. test2darray[1] is the same as (test2darray+30) -- it's the address of the beginning of the second 30-char chunk of those 30000 bytes. Since you told the compiler you will store chars there, it won't let you store a pointer there. (Well, not without some trickery, anyway.)

strcpy works because it actually copies char by char from the source string into the destination array.

This post has been edited by r.stiltskin: 17 March 2011 - 03:09 PM

Was This Post Helpful? 1
  • +
  • -

#3 musiliu  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 110
  • Joined: 04-December 09

Re: C Question about Initializing 2D Char Array

Posted 17 March 2011 - 02:56 PM

oh, so this is the case for 1D arrays too?
is strcpy the only way to assign the array if i don't assign it when initializing the array?
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: C Question about Initializing 2D Char Array

Posted 17 March 2011 - 03:25 PM

Yes, it's true for all arrays. But strcpy is only for char arrays.

No, it's not the only way to assign values to an array -- you could for example write a loop to copy element by element from source to destination. And there are other functions with similar functionality, like strncpy, memcpy, memmove, strcat, strncat all of which do some sort of copying of data from one area to another.

In general, for arrays of types other than char, you write a loop in which you copy element by element from the source to the destination, although you can also use memcpy.

This post has been edited by r.stiltskin: 17 March 2011 - 03:31 PM

Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5800
  • View blog
  • Posts: 12,635
  • Joined: 16-October 07

Re: C Question about Initializing 2D Char Array

Posted 17 March 2011 - 03:25 PM

If this is a list of strings, make an array of char* and fill them in as needed:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *copyStr(const char *s) {
	char *p = malloc(strlen(s) + 1);
	strcpy(p,s);
	return p;
}

int main() {
	char *list[10000];
	int i, listSize=0;
	
	list[listSize++] = copyStr("this is a test");
	list[listSize++] = copyStr("this is another test");
	
	for(i=0; i<listSize; i++) {
		printf("%d. %s\n", i, list[i]);
	}
	
	return 0;  
}



At this point, a linked list might be a good idea. It depends on if you plan to use all those slots in the array or not.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1