Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

So here's the confusion, let's say I declare an array of characters

char name[3] = "Sam";

and then I declare another array but this time using pointers

char * name = "Sam";

What's the difference between the two? I mean they work the same way in a program. Also how does the latter store the size of the stuff that someone puts in it, in this case 3 characters?

Also how is it different from

char * name = new char[3];

If those three are different where should they be used I mean in what circumstances?

share|improve this question
1  
It is actually a C question. Even const isn't used. –  Klaim Nov 14 '12 at 10:27
    
@Klaim. Using the new keyword in C? –  mike30 Nov 14 '12 at 15:50
    
@mike Ah yes, didn't hit that. XD –  Klaim Nov 14 '12 at 18:29
add comment

4 Answers

up vote 1 down vote accepted

The first option only consumes the necessary space in memory for a 3 char string + the termination character.

EDIT: Thanks for pointing that out in the comments. This option will actually give out an error because you only have 3 positions to fill, meaning there is no space for the termination character.

The second option points towards the memory position where the new string starts. This has a variable size and needs to be manually terminated with a \0 character ( the termination character ). This is usually prefered due to its variable size. This means that it will only consume as much memory as you need, unlike with the first option, where if you store a 3 character length string in a 100 position array, you will still have 100 positions reserved in an array.

Using the second option means that you are using pointers instead of arrays, sometimes making it hard to notice where the errors are occurring, so special attention when using this.

share|improve this answer
    
thanks thats what I wanted to know –  nashmaniac Nov 14 '12 at 6:47
6  
In the first example, the termination character is not allocated to name (in C), and you get a compiler error in C++ (at least with the GNU compiler). –  Giorgio Nov 14 '12 at 13:56
add comment
  1. char name[3] = "Sam";
    define an array named "name" (of course, you can use name[] rather than [3]), and
    name[0] = 'S', name[1] = 'a', name[2] = 'm', name[3] = '\0';
  2. char * name = "Sam";
    define a pointer pointed to the address where "Sam" is (it is at the storage location of constant)
  3. char * name = new char[3];
    define a pointer pointer to the address which is at the free store or heap, where 3 characters will be saved.
share|improve this answer
    
Given char name[3], name[3] is out of bounds. It may or may not work as intended, and is almost certainly undefined behavior. name goes from [0] through [2]. And since when is new in C? –  Michael Kjörling Nov 14 '12 at 14:35
add comment

In your first variant you have an array of three characters in a variable called name and you can store at most 3 characters in that.

In your second variant, you have declared a character pointer which points to a sequence of three characters, i.e. "sam".

The major difference is that you cannot change the value of the first name pointer (location to which it points), using operators like ++, --, + and -.

But in second option you can change the location to which it points and you can assign it to something with more length later on in the program like:

char * name= "sam";
/*some code goes here*/
name="hello";

In other words, name is constant in your first variant.

In the last variant you are just allocating a space of three characters and assigning the base address to the name pointer.

share|improve this answer
    
As mentioned in a response to the other question, it's very important to note that the second variant (and the constant "sam" in general) is a sequence of four characters, the last being the null terminator. –  Steven Burnap Dec 5 '12 at 18:04
add comment

In addition to what others have explained, I would like to illustrate the first example a bit more in detail. As Mohit Sehgal pointed out, in C the declaration

char name[3] = "Sam";

only allocates three bytes for the variable name, and puts no 0 string terminator: name[0] == 'S', name[1] == 'a', name[2] == 'm', name[3] == whatever is contained at the next address after that last (third) byte of name. For example, try out the following program:

#include <stdio.h>

char s[3] = "abc";
char t[]  = "def";

int main(int argc, char *argv[])
{
  printf("s = '%s'\n", s);
  printf("t = '%s'\n", t);
}

The result will be:

s = 'abcdef'
t = 'def'

because t has been allocated right after s and s now points to a string that goes on until the 0 terminator of t is found.

If you declare an array with initialization but without dimension, enough space for all the data in the initialization list will be allocated. Therefore

#include <stdio.h>

char s[] = "abc";
char t[] = "def";

int main(int argc, char *argv[])
{
  printf("s = '%s'\n", s);
  printf("t = '%s'\n", t);
}

will print

s = 'abc'
t = 'def'

I have tested the same declarations in C++ (using the GNU compiler), and

char name[3] = "Sam";

gives a compiler error because the string initializer (which C++ considers of length 4, including the trailing 0) is too long for the variable.

So, both in C and C++, the compiler will not allocate more bytes than are specified in the declaration.

share|improve this answer
    
It might be worth noting that sizeof(s) returns 4, showing that space is reserved for the null terminator. –  Steven Burnap Dec 5 '12 at 18:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.