C simple arrays and pointers question

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

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 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

  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] = '';
  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

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

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật