Array of strings notation

What is the difference between <char** array> and <char* arra

Page 1 of 1

12 Replies - 1590 Views - Last Post: 28 July 2008 - 10:49 PM Rate Topic: -----

#1 cybrid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 07-September 05

Array of strings notation

Posted 28 July 2008 - 04:36 AM

Hello everyone:

I have what I think is a dumb question but I don't seem to be able to find an answer:

Both compile without problem:
char** array1 = {"one","two","three"};
char* array2[] = {"one", "two", "three"};



but when I try to acces them
 printf("%s", array1[0]); 


causes the program to crash.

while
 printf("%s", array2[0]); 
don't.

¿Why is it? ¿what difference is there bewteen the two ways?

Thanks in advance :D.

Is This A Good Question/Topic? 0
  • +

Replies To: Array of strings notation

#2 Teaser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 14-July 08

Re: Array of strings notation

Posted 28 July 2008 - 08:04 AM

Which compiler are you using? Does it definately compile without errors and warnings?
Was This Post Helpful? 0
  • +
  • -

#3 captainhampton  Icon User is offline

  • Jawsome++;
  • member icon

Reputation: 13
  • View blog
  • Posts: 548
  • Joined: 17-October 07

Re: Array of strings notation

Posted 28 July 2008 - 08:21 AM

I believe it is because array2 is a pointer to a pointer so you must dereference array2 before you can print it.
Was This Post Helpful? 0
  • +
  • -

#4 cybrid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 07-September 05

Re: Array of strings notation

Posted 28 July 2008 - 10:52 AM

View Postcaptainhampton, on 28 Jul, 2008 - 05:21 PM, said:

I believe it is because array2 is a pointer to a pointer so you must dereference array2 before you can print it.

Hmm, nice point, it could be; I'll try
Edited: Tried and it doesn't work :(

Teaser, I'm using MinGW (GCC) on Windows.

This post has been edited by cybrid: 28 July 2008 - 11:01 AM

Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is online

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: Array of strings notation

Posted 28 July 2008 - 11:21 AM

My question is, what compiler are you using that allows:
char** array1 = {"one","two","three"}; ?

in general char* array2[] = {"one","two","three"}; creates what is called a "ragged array" -- an array of pointers to a set of "const char *"

char** array1 -- is a scalar value... you can't use the array initializer to assign a value to a scalar.

example:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{

	char* array[] = {"one", "two", "three"};
	char** ptr_array = array;

	printf("%s\n", ptr_array[0]);

	return 0;
}


There are all kinds of differences between these:

char *array[]; an array of pointers to a char.
char **ptr; a pointer to a pointer to a char.
char array[][n]; an array of char[n] arrays.
char (*ptr)[n]; a pointer to a char[n] array. *Edit: had syntax wrong


These all have different uses.



in *theory* in order to get char** array1 = {"one","two","three"};
to work it would have to be: char** array1 = &{"one","two","three"};

but this just is crazy syntax isn't it! No compiler should accept that either.

This post has been edited by NickDMax: 28 July 2008 - 11:48 AM

Was This Post Helpful? 0
  • +
  • -

#6 cybrid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 07-September 05

Re: Array of strings notation

Posted 28 July 2008 - 12:14 PM

View PostNickDMax, on 28 Jul, 2008 - 08:21 PM, said:

My question is, what compiler are you using that allows:
char** array1 = {"one","two","three"}; ?

in general char* array2[] = {"one","two","three"}; creates what is called a "ragged array" -- an array of pointers to a set of "const char *"

char** array1 -- is a scalar value... you can't use the array initializer to assign a value to a scalar.

example:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{

	char* array[] = {"one", "two", "three"};
	char** ptr_array = array;

	printf("%s\n", ptr_array[0]);

	return 0;
}


There are all kinds of differences between these:

char *array[]; an array of pointers to a char.
char **array; a pointer to a pointer to a char.
char array[][n]; an array of char[n] arrays.
char *array[n]; a pointer to a char[n] array.

These all have different uses.



in *theory* in order to get char** array1 = {"one","two","three"};
to work it would have to be: char** array1 = &{"one","two","three"};

but this just is crazy syntax isn't it! No compiler should accept that either.

My apologies ^_^ I come from Java XD, then to get an array of strings, how should it be done?
Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is online

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: Array of strings notation

Posted 28 July 2008 - 12:56 PM

well if you are using constant strings (ones that don't change values) then
char *array[] = {"one", "two", "three"}; works well.

If you would like to use strings whose values change then the easest way is to declare fixed "max lengths"

char array[10][MAX_SIZE]; -- Array of 10 string of maximum size MAX_SIZE (remember that in C a string is "zero terminated" -- meaning that a string is long defined up until the first character equal to 0.

This is generally what I do in C programs.

Now if you need an array of dynamic strings of unknown sizes then you will want to use:
char *array[10]; and then use malloc() to allocate space for each string.

now if you need a dynamic array of dynamic strings then you would want to use
char **array; and use malloc() to allocate the array of pointers, and then use malloc() to allocate the space for the strings.

Generally when you want to pass and array of strings to a function you use "char **array"

Now if you are in C++ then you really want to use the string class, and containers such as a vector rather than standard C character strings. These will behave much more like the java you are used to.
Was This Post Helpful? 1
  • +
  • -

#8 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 854
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Array of strings notation

Posted 28 July 2008 - 01:06 PM

The C language doesn't have a built-in string type - you need to deal with character arrays. Your two options are to create a statically allocated, statically sized array, or dynamically allocate the memory as you need it.

Creating statically sized arrays is the simplest solution to get started with, but it puts hard limits on your program (Most of the time this isn't a problem, though you need to be careful, since it will inevitably waste space, and there's a fair chance that the limits will one day cause problems).
char array[4][80] = { "one" , "two" , "three" , "four" }; 


Creating dynamically sized arrays can be a more efficient and flexible option, but it needs very careful memory management, else you may end up with memory leaks and/or dangling pointers - both of which can cause strange, hard-to-trace bugs.
char* array[4];
array[0] = malloc ( sizeof(char) * 80 );
array[1] = malloc ( sizeof(char) * 40 );

free( array[0] );
free( array[1] ); 


Which is better depends on the program you're writing - If you're new to C, you might find the explicit memory management a little overwhelming to begin with, though its hard to escape once you leave the basics of the language.
Was This Post Helpful? 1
  • +
  • -

#9 cybrid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 07-September 05

Re: Array of strings notation

Posted 28 July 2008 - 02:31 PM

So if I have understood well, though
char** array1 = {"one","two","three"};
isn't well formed, doing
char *array[] = {"one", "two", "three"};
would give me in essence a pointer to an array of pointers to char arrays. Am I correct?
Was This Post Helpful? 0
  • +
  • -

#10 NickDMax  Icon User is online

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: Array of strings notation

Posted 28 July 2008 - 02:52 PM

Yes... but be warned that the initial sizes are important. array[0] has three characters. for example:
int main(int argc, char* argv[])
{

	char * array2[] = {"one", "two", "three"};

	array2[0][3]='-';
	printf("%s\n", array2[0]);

	return 0;
}
this outputs:
one-two

this is because array2[0][3] was the '\0' marking the end of the string "one". When this was removed the string continued until the next '\0' which marked the end of the second string in the array.

This is an important little note about ragged arrays.

This post has been edited by NickDMax: 28 July 2008 - 02:52 PM

Was This Post Helpful? 0
  • +
  • -

#11 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 854
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Array of strings notation

Posted 28 July 2008 - 02:59 PM

View Postcybrid, on 28 Jul, 2008 - 10:31 PM, said:

So if I have understood well, though
char** array1 = {"one","two","three"};
isn't well formed
Correct!

Quote

doing
char *array[] = {"one", "two", "three"};
would give me in essence a pointer to an array of pointers to char arrays. Am I correct?

No, it just gives you an array of pointer-to-char.

Reading declarations in C uses the 'right-left' rule. The right-left rule indicates that you should read to the right of the variable name first before reading to the left. So for char* array[] begin at the identifier (the variable name);

char* array[] - The variable named array is an ...
char* array[] - array of ...
char* array[] - pointer-to ...
char* array[] - char


To get a pointer-to an array requires more complicated syntax. If you wanted a pointer-to-array-of-pointer-to-char, the declaration would look like this; char* (*array)[]
Parenthesis around a particular element in a declaration generally causes you to switch direction when reading with the right-left rule.

Here's how to read it
char* (*array)[] - The variable named array is ...
char* (*array)[] - **stop here and switch direction
char* (*array)[] - a pointer-to ...
char* (*array)[] - **stop here and switch direction
char* (*array)[] - array of ...
char* (*array)[] - pointer-to ...
char* (*array)[] - char

This post has been edited by Bench: 28 July 2008 - 03:08 PM

Was This Post Helpful? 0
  • +
  • -

#12 NickDMax  Icon User is online

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: Array of strings notation

Posted 28 July 2008 - 06:43 PM

ah... didn't catch that... sorry. Thanks for the pick up Bench. :) Always keeping me honest.
Was This Post Helpful? 0
  • +
  • -

#13 cybrid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 07-September 05

Re: Array of strings notation

Posted 28 July 2008 - 10:49 PM

And I thought I already knew all that I needed to know about pointers LOL; thank you both for this enlightening explanation :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1