How to Assign a Value to the Pointer 'This' in C++

Directly assigning values to C Pointers

The problem is that you're not initializing the pointer. You've created a pointer to "anywhere you want"—which could be the address of some other variable, or the middle of your code, or some memory that isn't mapped at all.

You need to create an int variable somewhere in memory for the int * variable to point at.

Your second example does this, but it does other things that aren't relevant here. Here's the simplest thing you need to do:

int main(){
int variable;
int *ptr = &variable;
*ptr = 20;
printf("%d", *ptr);
return 0;
}

Here, the int variable isn't initialized—but that's fine, because you're just going to replace whatever value was there with 20. The key is that the pointer is initialized to point to the variable. In fact, you could just allocate some raw memory to point to, if you want:

int main(){
void *memory = malloc(sizeof(int));
int *ptr = (int *)memory;
*ptr = 20;
printf("%d", *ptr);
free(memory);
return 0;
}

How can I assign the value to pointer?

When you say

int *head;

the name of the variable you are defining is head, and its type is "pointer to int", or int *.

When you say

int *head = NULL;

you're declaring the same variable head, and you're also giving it an initial value of NULL.

But since the name of the variable is head, if you want to define it on one line and then give it a value on a second line, do it like this:

int *head;
head = NULL;

But, it's true, this can be confusing at first.

For ordinary variables, you can easily imagine saying

int i = 5;

but then later changing it to

int i;
i = 5;

So if you say

int *head = NULL;

it's natural to want to change it to

int *head;
*head = NULL;

But this is wrong.

A big part of the confusion is that the * character ends up
doing two different things.

When you say

int *head;

what the * is doing is saying that you're declaring a pointer.
You're saying, "I'm declaring a variable named head, and it's a
pointer, and what it points to is int."

But when you say

*head = NULL;

what the * is doing is saying "Take the pointer head, and figure out
what it points to, and set the pointed-to location to NULL."

So when you say *head = NULL, you are not setting head to
NULL; you are setting the pointed-to location to NULL. And
that's a very different thing.

Bottom line: What you want here is

head = NULL;

Assigning value to the address pointed to by pointer? C++

The exact question: Why do you write *pointername = newValue; instead of pointername = newValue?

These are two quite different things. (And you can do both.)

int i = 42;
int x = 23;
int * p = &i;

Now, I can change the content of the location I am pointing to...

*p = 123; // this changes the value of i

Or I can change the location I am pointing to.

p = &x; // now p points to x instead

The "natural" value of a pointer variable is a memory address. So, if I assign to the "naked" p, I am assigning a memory address (which I can obtain, for example, by using the & operator on a variable).

Using the operator *, I dereference the pointer variable. So, if I assign to the "combined" *p, I assign a value of the type pointed to (which ends up at the memory address contained in p).

Treat a variable value as an address and assign this value to a pointer

The "most correct" way convert between an integer to a pointer without any warnings, you have to cast to uintptr_t, then to void*, then to your pointer type.

uint32_t b = 1;
uint32_t *p = (uint32_t*)(void*)(uintptr_t)b;

But usually programmers just do macros, that are properly cast:

#define A_ADDR  ((uint32_t*)0x0000000A)
#define B_ADDR ((uint32_t*)0x0000000B)
uint32_t *p = A_ADDR;
p = B_ADDR;

which compiles without a warnings on gcc -Wall -Wextra -pedantic.

Or sometimes just macros with constant numbers, and the user is supposed to cast to (void*) or to proper type:

#define A_ADDR 0x0000000A
#define B_ADDR 0x0000000B
uint32_t *p = (uint32_t*)A_ADDR;
p = (uint32_t*)B_ADDR;

Alternatively, if you trust your compiler, they can be static const variables, ex. static const uint32_t *A_ADDR = (uint32_t*)0x0000000A;

Assigning values to a pointer

In your example this is a compiler error.

However what I assume you wanted to do was this:

int n =23, *p;
p = &n;
//Change the value of p to 3000, p now points to address 3000
p = reinterpret_cast<int*>(3000);
//Check if the address of n has changed
std::cout << "Address of n : " << reinterpret_cast<int>(&n) << std::endl;

As you can tell when you run this code. The address of n does not change.

For your second question.

Yes and No :)

If you define two variables next to each other they may be next to each other in memory.

 int a,b,c,d;
char c = 1;
short s = 1;
void* p = nullptr;
int i = 1;

std::cout << "a is at: " << reinterpret_cast<int>(&a) << std::endl;
std::cout << "b is at: " << reinterpret_cast<int>(&b) << std::endl;
std::cout << "c is at: " << reinterpret_cast<int>(&c) << std::endl;
std::cout << "d is at: " << reinterpret_cast<int>(&d) << std::endl;
std::cout << "Char is at: " << reinterpret_cast<int>(&c) << std::endl;
std::cout << "Short is at: " << reinterpret_cast<int>(&s) << std::endl;
std::cout << "Pointer is at: " << reinterpret_cast<int>(p) << std::endl;
std::cout << "Int is at: " << reinterpret_cast<int>(&i) << std::endl;

This behavior is caused the compiler determining where to stick everthting. It may or may not exist next to each other. If you want to guarantee they exist next to one another, use an array.

int arr[] = {1,2,3,4,5,6,7};
int * p = &arr[0]; //get address of first element
for(int i = 0 ; i < 7; ++i)
std::cout << "Value at address: " << reinterpret_cast<int>(p+i)
<< " is: " << *( p + i) << std::endl;


Related Topics



Leave a reply



Submit