1

When we want to have a pointer for integer we add &(int *p = &n)

int n=50;
int *p = &n;

, so similarly in case of a string shouldn't it be the same as this,

char name[] = "Hello"
char *s = &name;

or like this code below,

char *s= &"Hello";

rather than like this,

char *s= "Hello";

OR

char name[] = "Hello";
char *ptr=name;

OR

char name[] = "Hello";
char *ptr;
ptr= name;

I don't think the last two codes are different.

  • 1
    `char name[]` and `char name*` are the same thing. Probably stems from the whole idea that arrays decay to pointers. Also, there is no `c/c++` language. Choose one. – super Jun 21 '20 at 17:21
  • @super This question is completely `c/c++` agnostic, so, IMO, it is appropriate to have both tags – Craig Estey Jun 21 '20 at 17:25
  • @super They're not, actually. At least in c++, which you'll find out if you try to call a function, which takes a pointer, with a string literal. `char[]` (or any array type) _decays_ to a pointer to the first element at the slightest provocation, though. Agree, however, that the question should make clear which language is being asked about. – ravnsgaard Jun 21 '20 at 17:28
  • @super -- they're not the same thing. `const char *name = "abcd";` creates a pointer that points at a literal string. `const char name[] = "abcd";` creates an array of 5 char that holds the five characters `'a'`, `'b'`, `'c'`, `'d'`, and `'\0'`. – Pete Becker Jun 21 '20 at 18:19
  • Since you tagged as C++, you should be using `std::string` for text, so that you can avoid these pointer issues. – Thomas Matthews Jun 21 '20 at 18:41
  • now I have removed C++ , I didn't knew it would be different in c++ – KARAN GUPTA Jun 22 '20 at 07:33

3 Answers3

3

In this version:

int n = 50;
int *p = &n;

you have a single int n. To make a pointer to it, you need to take its address with &.

On the other hand, in this version:

int n[50];
int *p = n;

the variable n is an array of int. This has the property that the name of the array actually refers to the first element (i.e. the array decays to a pointer when used like this). So no & is needed to refer to the address.

The exact same logic applies to the char case:

char name[] = "Hello";
char *ptr = name;

where name is an array, so you can take a pointer to the first element by just using the variable name without using &.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
cigien
  • 57,834
  • 11
  • 73
  • 112
1

char *s = &name; does not work because the pointer types do not match. When you apply the "address of" operator to an array, what you get is a pointer to an array. But the pointer variable that you are initialising is not a pointer to an array. It is a pointer to a char.

You can use the exactly same pattern that you used with integer to get a pointer to an array:

char (*s)[6] = &name;

Alternatively, when you want a pointer to an element of an array, you can use the subscript operator to get the element, and apply addressof on that element:

char *s = &name[0];

But char *s = name; also works because the array implicitly converts to pointer to first element. This implicit conversion is called decaying.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

Given char name[] = "Hello";, if you want the address of name, you should use &name. That provides a pointer to an array of six char.

The compiler should warn about char *s = &name; because it uses a pointer to an array for a pointer to a char, and those are different (and incompatible) types of pointers.

In C, we generally do not work with arrays. Instead, we work with pointers to array elements. To assist with this, C automatically converts an array to a pointer to its first element, except in certain situations.

Thus char *s = name;, name is automatically converted to a pointer to its first element, which is a char. This is the reason we do not need to use & with arrays, even though we need to use & to take the address of a non-array—with an array, C automatically gives us an address, so we do not need to use &.

Although &name points to the start of the array and name points (after conversion) to the first character of the array, which starts in the same place, the pointers have different types.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • `char name[]` is *already* a pointer to an array of six `char`. – Robert Harvey Jun 21 '20 at 17:30
  • 1
    @RobertHarvey: No, after the definition `char name[] = "Hello";`, `name` is an array of six `char`. This can be seen by printing the result of `sizeof name`, which will be 6. – Eric Postpischil Jun 21 '20 at 18:01
  • Well, yes; it has size information. But to get a pointer to the array, it's not `char* array = &name;` it's just `char* array = name;` – Robert Harvey Jun 21 '20 at 18:07
  • To put it another way, why would you ever need `char* array = &name;`? – Robert Harvey Jun 21 '20 at 18:10
  • @RobertHarvey: By the rules of the C standard, `name` designates the **array**. It is not a pointer. You can think of it as a pointer if you wish, but it will not serve you well because the rules of the standard are written to treat it as an array, and regarding it otherwise will cause problems, and it is a false statement that you should not teach to other people. – Eric Postpischil Jun 21 '20 at 19:30
  • @RobertHarvey: As for when pointers to arrays arise, one situation is in multidimensional arrays. Another is, if a routine takes a parameter that is an array of 5 `float`, it gets type checking to guard against a caller accidentally passing fewer. Regardless of whether you think it has use or not, the rules of the C standard are clear. Arrays are not pointers. – Eric Postpischil Jun 21 '20 at 19:31
  • I guess what I'm saying is that your answer would be a much better one if you simply eliminated the first paragraph. It does not illuminate, and I don't have any quarrel with the rest of your answer. – Robert Harvey Jun 21 '20 at 20:33