-2

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?

3
  • 1
    It is actually a C question. Even const isn't used.
    – Klaim
    CommentedNov 14, 2012 at 10:27
  • @Klaim. Using the new keyword in C?
    – mike30
    CommentedNov 14, 2012 at 15:50
  • @mike Ah yes, didn't hit that. XD
    – Klaim
    CommentedNov 14, 2012 at 18:29

4 Answers 4

1

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.

1
  • 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
    CommentedNov 14, 2012 at 13:56
1
  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.
1
  • 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?
    – user
    CommentedNov 14, 2012 at 14:35
0

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.

1
  • 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.
    – user53141
    CommentedDec 5, 2012 at 18:04
0

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.

1
  • It might be worth noting that sizeof(s) returns 4, showing that space is reserved for the null terminator.
    – user53141
    CommentedDec 5, 2012 at 18:08

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.