Vincenzo Gibiino Vincenzo Gibiino - 9 months ago 54
C++ Question

Request explanation on the behaviour of pointers in array

I m running this using rextester (online compiler). I followed a tutorial but there is something I don't understand.

I thought it would be better to write my question directly inside the code.

//gcc 5.4.0

#include <stdint.h>
#include <stdio.h>

uint8_t var1 = 17;
uint8_t var2 = 23;
uint8_t arr[] ={7,9,11};

uint64_t *ptr1;//pointer
uint64_t *ptr2;
uint64_t *ptr3;

int main(void)
{

printf("var1: %d\n", var1) ;

//connecting pointer to address
ptr1 = &var1;
printf("address of ptr1: %d\n", ptr1) ;
printf("value of ptr1: %d\n\n", *ptr1) ;

//connecting pointer to address + 1
ptr2 = &var1 +1;
printf("address of ptr2: %d\n", ptr2) ;

//assign value to pointer
*ptr2 = var2;
printf("value of ptr2: %d\n\n", *ptr2) ;

//try on array
ptr3= &arr;//no need to point element 0, or yes?
printf("address of ptr3: %d\n", ptr3) ;
printf("value of ptr3: %d\n\n", *ptr3) ;//i expect 7
return 0;
}


Any help would be very appreciate to help me understand the right behaviour of pointers in c and cpp
I made a lot of tries but i m not able to link a pointer to an array

Edit after response of mato:

Do you think this is a clean way to work with pointer and array? Or there are better solution which take care of not overwriting memory?

//gcc 5.4.0

#include <stdint.h>
#include <stdio.h>

uint16_t var = 17;
uint16_t arr[] ={3,5,7,11,13};
uint16_t *ptr;

int main(void)
{

printf("var: %d\n", var) ;

//connecting pointer to address
ptr = &var;
printf("address of ptr: %d\n", ptr) ;
printf("value of ptr: %d\n\n", *ptr) ;

//try on array
for (uint16_t n =0;n<5;n++){
ptr= &arr[n] ;
printf("item: %d value: %d ads: %d pointer: %d\n", n, arr[n], ptr, *ptr) ;
}
return 0;
}

Answer Source

It seems that you do understand what pointers are and you can use them with basic types.

There are two problems in your code. First is this part:

//connecting pointer to address + 1
ptr2 = &var1 + 1;

Here you assigned some address to variable ptr2. Up to this point there is nothing dangerous about that.

But then you assign a value to memory at that address

//assign value to pointer
*ptr2 = var2;

This is dangerous because you, as a developer, don't know what is stored at that address. Even if you are lucky right now, and that part of memory isn't being used for anything else, it will most likely change once your program gets longer and then you will have hard time searching for the bug.

Now arrays usually are a bit confusing, because when you create an array like this:

uint8_t arr[] = {7,9,11};

three things happen.

  1. Your program allocates continual block of memory, that fits 3 variables of type uint8_t. The 3 variables in this context are called elements.
  2. The elements will get the provided initial values 7, 9 and 11.
  3. An address of first element (the one that contains value 7) will be stored in arr.

So arr is actually of type uint8_t *.

In order to get the last part do what you expect, you just need to change this one line (remove the &):

ptr3 = arr;

EDIT: BTW watch and understand this course and you will be expert on C memory manipulation. Video is a bit dated, but trust me, the guy is great.

EDIT2: I just realised the other answer is absolutely correct, you really need to match the types.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download