0

Say I have the following problem:

main(void) {
int * p;
int nums [3] = {1,5,9};
char c [3] = {'s','t','u'};
p =  nums [2];
*p = (int) *c;
}

What does the last line mean?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261

4 Answers4

1

Let's break it down: *p = (int) *c;

c is a char array.

*c is the first element of the char array, because c[0] = *(c+0) = *(c) = *c

(int) *c casts the first element of the char array c to an integer. This is required, because with...

*p = (int) *c you assign the to an integer casted char to the content of pointer p.

Peter Merkert
  • 354
  • 5
  • 14
1

This code will not work, or will cause problems if it does.

the line; p = nums[2];

sets the value of the pointer p to the value 9. This is not likely a legal value for your pointer. If it were, then the memory location 9 would be set to 115 which is the integer value of 's'.

john elemans
  • 2,578
  • 2
  • 15
  • 26
0

*cDecay c to pointer-to-first-element, and access the pointed-to value. Same as c[0].
(int) *c → cast that value to int.
*p = (int) *c → assign that to what p points to.

Community
  • 1
  • 1
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • But when you say *c...it is an array. So does it always point to the first index? –  Oct 14 '15 at 21:57
  • So *c means the pointer to the memory location of the first index of the array? –  Oct 14 '15 at 22:00
  • An array is an array, not a pointer. An array doesn't point anywhere. But an array can decay to a pointer to the first element in the array, as happens when the `*` dereference operator is applied to the array name. Thus `*c` evaluates to the first element of the array, and is identical to writing `c[0]`. – Emil Laine Oct 14 '15 at 22:01
  • *c = c [0] is what you are saying –  Oct 14 '15 at 22:02
  • Yes, when you define an array, the array name is a pointer to the array's first element, so in your specific example for instance, nums is a pointer to num[0]. That is to say *nums = nums[0]. – Samidamaru Oct 14 '15 at 22:03
  • @MPI_What No, the array name is not a pointer. The array name is a name for the array, and the array itself is an array, not a pointer. – Emil Laine Oct 14 '15 at 22:04
  • An array name is essentially a constant pointer to the first element of the array... – Samidamaru Oct 14 '15 at 22:05
  • @MPI_What Time to learn the basics. An array name can __decay__ to a pointer-to-the-first-element when it is handled like a pointer (e.g. dereferenced), but it __is__ not a pointer. See [Is array name a pointer in C?](http://stackoverflow.com/q/1641957/3425536). – Emil Laine Oct 14 '15 at 22:07
  • Very well, I accept. My point was more that they can be treated as the same thing though. I'll butt out now ... – Samidamaru Oct 14 '15 at 22:09
  • Sometimes yes, sometimes no. – Emil Laine Oct 14 '15 at 22:10
0

There are many issues in this code, let's address them first.

  • Firstly, main(void) is not conforming code. You need to change that to int main(void).

  • Secondly, p = nums [2]; is wrong. p is of type int *, and nums[2] is of type int. You may not assign an int to a int * and expect something fruitful to happen. Maybe what you meant to write is p = &nums[2];. Without this modification, going further will invoke undefined behavior as you will try to access a memory location that is invalid to your program.

Then, coming to your question,

*p = (int) *c;

it basically dereference cNOTE to get the value, then cast it to an int type and store into the memory location pointed by p. However, in C, this casting is not required. The above statement is equivalent to

*p = *c;

anyway.


NOTE: Array name decays to the pointer to the first element of the array, i.e., in this code, using c is the same as &c[0], roughly.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261