Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,148 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,066 people online right now. Registration is fast and FREE... Join Now!




Understanding Pointers and Arrays: dereferencing in C

 
Reply to this topicStart new topic

Understanding Pointers and Arrays: dereferencing in C, Understanding Pointers and Arrays: dereferencing in C

katana
5 Jun, 2007 - 11:13 AM
Post #1

New D.I.C Head
*

Joined: 18 May, 2007
Posts: 6


My Contributions
CODE

main()
{
  char *p;
  char buf[10] ={ 1,2,3,4,5,6,9,8};
  p = (buf+1)[5];
  printf("%d" , p);
}


Here p is a pointer and an integer is assigned to a pointer and when printed though it is a pointer it is printing the value at the address.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Understanding Pointers And Arrays: Dereferencing In C
5 Jun, 2007 - 12:57 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
QUOTE
Here p is a pointer and an integer is assigned to a pointer and when printed though it is a pointer it is printing the value at the address.


p is a pointer to a char, and an attempt is made to assign it to the char 9 (tab char incidentally). This would make p a pointer to the address 9 in memory which would be a bad thing if you were to attempt to use the pointer.

The about program should give you an error telling you that it can not convert char to char *.

Here again we see that pointer and arrays are closely linked. An array variable is a pointer to the beginning of the the array. The operator [ ] works like an alias for the dereferencing operator *. So Array[i] is the same thing as *(Array + i).

So the line p = (buf + 1)[5] translates to p = *((buf + 1) + 5). Which is the same as p = *(buf+6) which is the same as p = buf[6] (which is why you should get the error because buf[6] is of type char, and p is of type char *.)

This might help:
CODE
int main()
{
  char *p;
  char buf[10] ={ 1,2,3,4,5,6,9,8};
  // p = (buf+1)[5]; // Type conversion error. p is of type char *, and (buf + 1)[5] is of type char
  p = &(buf+1)[5];
  printf("p = %d,\t *p = %d\n" , p, *p);
  printf("buf = %d,\t (buf + 1) = %d,\t &buf[1] = %d\n" , buf, buf + 1, &buf[1]);
  printf("*buf = %d,\t *(buf + 1) = %d,\t buf[1] = %d\n" , buf, buf + 1, buf[1]);
  printf("((buf+1)+5) = %d,\t *((buf + 1) +5) = %d,\t buf[6] = %d\n" ,((buf+1)+5), *((buf + 1) +5), buf[6]);
  return 0;
}

User is offlineProfile CardPM
+Quote Post

katana
RE: Understanding Pointers And Arrays: Dereferencing In C
5 Jun, 2007 - 02:26 PM
Post #3

New D.I.C Head
*

Joined: 18 May, 2007
Posts: 6


My Contributions
Instead of showing error, it is printing the value of the corresponding element in the array.A further insight into the problem would be helpful to me.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Understanding Pointers And Arrays: Dereferencing In C
5 Jun, 2007 - 06:13 PM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
Ummm... What compiler are you using? And you could you post the output?

That program should not compile correctly (unless I am all wrong). -- AND I am wrong, the program compiles just fine when I select C rather than C++.
instead it gives me a warning:
warning C4047: '=' : 'char *' differs in levels of indirection from 'char '

The program should print out '9'.

Now keep in mind that '9' is an address, not a value.
For example the program
CODE
#include <stdio.h>

int main()
{
  char *p;
  char buf[10] ={ 1,2,3,4,5,6,9,8};
  p = (buf+1)[5];
  printf("p = %d, buf[6] = %d, &buf[6] = %d\n", p,  buf[6], &buf[6]);
  return 0;
}
produces the output:
p = 9, buf[6] = 9, &buf[6] = 1245046
I cannot print out *p since the program does not have rights to access this area of memory, but if I could the chances are it would not be 9.

again (buf+1)[5] is the same thing as *((buf+1)+5) = *(buf+6) = buf[6] and buf[6] is a char, not a char *. If your compiler made p == &buf[6] then you should get another compiler because yours is broken (well non-standard really).
User is offlineProfile CardPM
+Quote Post

katana
RE: Understanding Pointers And Arrays: Dereferencing In C
6 Jun, 2007 - 12:36 AM
Post #5

New D.I.C Head
*

Joined: 18 May, 2007
Posts: 6


My Contributions
i am using gcc compiler and the output is 9

I still dont understand u saying that it prints 9 and it is an address...how do that 9 comes into picture
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Understanding Pointers And Arrays: Dereferencing In C
6 Jun, 2007 - 02:06 AM
Post #6

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
buf[6] = 9. It is an char whose numeric value is 9 (so it is the tab character).

buf is a pointer to the character buf[0]. (buf+1) is a pointer to the character buf[1].

The operator [] is equivalent to a dereference of an offset. So buf[x] equivalent to *(buf+x).

if you don't believe me on this: try using the notation x[buf], for example 6[buf] == 9 because *(6+buf) = *(buf+6) = buf[6] = 9.

So the notation (buf+1)[x] becomes *((buf+1)+x), which is equivalent to *(buf+(x+1)), which is equivalent to buf[x+1]. So (buf+1)[5] is the same thing as writing buf[1+5] which is the same thing as buf[6].

So p = (buf+1)[5]; is the same thing as writing p = buf[6]; Since buf[6]==9, this means that p==9.

Now the important thing to remember is that buf[6] is a value. p is a pointer. So if p=9 then p is a pointer that points to the address 9, and the VALUE *p is equal to whatever is stored at that address.

So when p prints out as the number 9, you have to remember that p is not an integer, it is a pointer. a pointer points to something. It is the address of something. If p=1 then p points to the first byte in the computers memory (well technically the first byte in the current memory scheme -- and assuming that a char is 1 byte). If p=10 then p points to the 10'th byte. If p = 1024 than p points to the second kilobyte. Etc.

Lets say we left p=(buf+1)[5]; then we asked the computer to print out p[buf-9+i] where i went from 0 to 7.

well p[buf-9+i] = *(p+buf-9+i) = *(9+buf-9+i) = *(buf+ (9-9) +i) = *(buf+i)=buf[i]

CODE
#include <stdio.h>

int main()
{
  char *p;
  char buf[10] ={ 1,2,3,4,5,6,9,8};
  int i, offset;
  p = (buf+1)[5];
  printf("p = %d, buf[6] = %d, &buf[6] = %d\n", p,  buf[6], &buf[6]);
  for (i=0; i<8; ++i)
  {
      offset = (int)buf-9 + i;
      printf("%d, ", p[offset]);
  }
  printf("\n");
  return 0;
}


so we would expect (and indeed we will see) the output of the program to be:
1, 2, 3, 4, 5, 6, 9, 8 which is indeed the original array.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:56PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month