Comparing two ways of initializing an int pointer - c

Suppose I have a pointer
int *x;
Then I need to let the pointer point to an int of value, say 42.
Should I do this:
*x = 42;
or this:
int y = 42;
x = &y;
? What is the usual practice?

After this declaration
int *x;
the pointer x either is equal to NULL (if it is declared outside any function) or has an indeterminate value. So dereferencing the pointer like
*x = 42;
invokes undefined behavior.
You can write either
int y = 42;
x = &y;
or
x = malloc( sizeof( int ) );
*x = 42;

When you do:
int *x = 42;
you assigne the pointeur x to the memory case 42, that gonna make a segmentation fault.
The right way to do what you wan't is:
int y = 42;
x = &y;

x gets the adress of y (&y)
#include <stdio.h>
int main()
{
int y=42;
int x=&y;
printf("\nY = %d is at adress X = %d \n",y,x);
}

Related

Pointer and address issue in C

Suppose that in the main function an int type varaible x has a value 20. IF the function is called 2 times as foo(&x) , whats the value of x?
#include<stdio.h>
void foo(int *n)
{
int *m;
m = (int *)malloc(sizeof(int));
*m = 10;
*m = (*m)*5;
n = m;
}
int main()
{
int x = 20;
foo(&x);
printf("%d",x);
}
Shouldn't the value of x be 50 since we are initializing the address of n with m which has the value 50 but its coming out to be 20?
The address of the n pointer is local to foo. So modifying the pointer inside foo has no effect outside of the function. But when dereferencing n, the pointed-to value can be changed.
For x to become 50, the last line of the foo function should have been:
*n = *m;

Pointer to pointer as argument in a function

I have the following code:
void change_adrs(int **q)
{
int * otheraddess;
*q = otheraddress;
}
I can't understand this assignment: *q = otheraddress;
Could anybody help me to explain this. Why can't we use another assignment, such as:
**q = otheraddress;
*q = *otheraddress;
q = &otheraddress;
void change_ptr(int **q)
{
*q = malloc(somemem);
}
void foo()
{
int *ptr;
change_ptr(&ptr);
}
in this example it will change the value of the ptr itself. When you pass a single star pointer you can only change the referenced object.
Look at the assignment of *q = otheraddress; the same as you look at the following:
int x = 4;
int y = x;
you take 2 vairables of the same type and make an assignment between both of them.
In your case, you use the address of int vairables.
int *otheraddress; is a vairable who can get an address (& operator) to int vairable such as:
int x = 4;
int *p = &x; //p<=the address of x
and int **q; can get an address of a vairable which can get int address. such as you 3rd assignment (which should work fine):
int x = 4;
int *p = &x;//p<=the address of x
int **q = &p;//q<=the address of p
for the other assignmets:
**q = otheraddress; you try to assign int* into int**
*q = *otheraddress; you try to assign int into int *
After declaring the variables, * dereferences them.
For example after declaring something like int **a;, when using it we have:
**a value in value in location stored in a(which is ultimately an int)
*a value in location stored in a (which is another location)
a location stored in a
&a location of a itself
Lets take a look and see why each of your examples don't work:
**q = otheraddress; assigning a location to an int ×
*q = *otheraddress; assigning an int to a location ×
Edit: Lets take a look at these two examples:
q = &otheraddress; assigning a location to a location ✓
*q = otheraddress; assigning a location to a location ✓
Lets look at it this way:
Suppose we have:
int *a;
int **q;
in order to prevent segmentation faults in the future, lets first assign some address to a:
int t = 5
a = &t;
Lets look at the addresses afterq = &a:
//first snippet
0x7fffc3a2b338 // &a
0x5596853c98c0 // a
5 // *a
0x7fffc3a2b340 // &q
0x7fffc3a2b338 // q
0x5596853c98c0 // *q
5 // **q
As expected, &a is put into q, and a holds &t, so **(&a) and **(q) will both hold a value of 5.
Now lets look at the addresses after*q = a:
//second snippet
0x7fffc3a2b338 // &a
0x5596853c98c0 // a
5 // *a
0x7fffc3a2b340 // &q
0x7fffc3a2b345 // q
0x5596853c98c0 // *q
5 // **q
Here, a is put into *q, and a itself is &t, so *(a) and *(*q) with both hold a value of 5.
To understand the difference, we look at an example:
int b = 3;
a = &b;
Using this after the first snippet, a is given another address, and q was declared &a(which hasn't changed), therefore **(q) has the same value as **(&a), which is now 3.
This is just like saying:
int a = 3;
int *b = &a;
a = 5; //even though a has changed, b retains its old value of &a, thus *(b) == *(&a) == 5
However on the second snippet, *q was already declared as a before it had changed, so even though a now has a new address, the address inside *q still hasn't changed. Thus trying to access *(*q) will use the old address and give us 5 again.
This is just like saying:
int a = 3;
int b = a;
a = 5; //even though a has changed, b still retains it's old value of 3

Why the answer of following code is 1000? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I came across a question on pointer which is given below. I am not able to understand why the output of the code below is 1000 and not 10. Can anyone help me how it is possible?
#include<stdio.h>
int y;
void main()
{
int x, *px, **ppx;
x = 10;
y = 1000;
px = &x;
ppx = &px;
f3(ppx);
printf("%d", *px);
}
void f3(int **pp)
{
*pp = &y;
}
This statement:
px = &x;
assigns the address of x to px. This statement:
ppx = &px;
assigns the address of px to ppx. This statement:
f3(ppx);
passes the value of ppx to f3. In f3, this statement:
*pp = &y;
assigns the address of y to the place that pp points. pp is the parameter of f3, which was given the value ppx in the call.
That is, pp is ppx, which points to px. Since pp points to px, *pp = &y puts the address of y in px.
Now px points to y. Then this statement:
printf("%d", *px);
prints the thing that px points to, which is y, which is 1000.
This statement *pp = &y; causes to print 1000. Before calling f3() px points to address of x as px=&x; but in f3() px got modified as address of y.
int y;
void f3(int **pp){ /*since you didn't declare functin,
so either declare it or define before main() */
*pp = &y;/* px contains now address of y */
}
int main(){ /* use int main() instead of void main() */
int x,*px,**ppx;
x=10;
y=1000;
px=&x;/* px points to address of x*/
ppx=&px; /* ppx points to address of px */
f3(ppx); /* passing ppx i.e address of px
And in f3() *pp = &y means px got replaced by
address of y */
printf("%d",*px); /* prints value at address of y, not address of x */
return 0;
}
Compile your code always with -Wall flag, don't ignore warnings.
You can change an object in a function passing it by reference that is indirectly through a pointer.
For example
#include <stdio.h>
void f( int *p, int value )
{
*p = value;
}
int main(void)
{
int x = 10;
printf( "Before call f x = %d\n", x );
int *px = &x;
f( px, 20 );
printf( "After call f x = %d\n", x );
return 0;
}
The program output is
Before call f x = 10
After call f x = 20
In the program the function deals with a pointer to the variable x declared in main. That is the pointer points to the variable x. Dereferencing the pointer you get access to the variable and thus can change it.
*p = 20;
The same way you can change an object that has a pointer type. To do this you have to pass the object/pointer by reference as it was shown above.
For example
#include <stdio.h>
typedef int * T;
void f( T *pp, T p )
{
*pp = p;
}
int main(void)
{
int x = 10;
T px = NULL;
T *ppx = &px;
f( ppx, &x );
printf( "After call f *px = %d\n", *px );
return 0;
}
The program output is
After call f *px = 10
At the beginning of the program the pointer px that has the type int * (due to the typedef) is initialized by NULL. Then it is passed to the function f by reference
T *ppx = &px;
f( ppx, &x );
that is indirrectly through a pointer to the pointer px. Within the function the pointer px is reassigned with the address of the variable xby dereferencing the pointer pp that points to the original pointer px.

Using pointers in C programming?

Right now I'm working on a program in C that takes 3 parameters; the address of a "first" integer, address of the "second" integer and the address in which to store the maximum of the two integers. So far I have the following basic code:
void max(int* x, int* y, int* z)
{
if (x > y) {
z = x;
}
else if (y > x){
z = y;
}
}
int main()
{
int x = 6;
int y = 4;
int z;
max(&x, &y, &z);
printf("max of %d and %d = %d\n", x, y, z);
x = 12;
y = 17;
max(&x, &y, &x);
printf("x = %d, y = %d\n", x, y);
}
When executed it outputs the following:
max of 6 and 4 = 32767
x = 12, y = 17
HOWEVER! I want it to output this instead:
max of 6 and 4 = 6
x = 17, y = 17
I'm not sure where I'm going wrong in my max function. Z should not be so huge and in the second print statement x should equal y. Any help is greatly appreciated, thanks!
As you probably already know, a pointer is a variable which contains the address in memory of another variable.
To access that memory we use The Unary operator & which gives us the address of a variable.
But accessing that memory is not all what a Pointer can do, we can with that pointer modify the value of that variable where the pointer points to.
For that we need to supply * to that pointer like this *ptr.
Let's take a look at the following program:
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
int main(void){
int a = 5;
int b = 10;
printf("A = %d\nB = %d\n\n\n",a,b);
int *pa = &a;
int *pb = &b;
*pa = 50;
*pb = 100;
printf("A = %d\nB = %d\n*PA = %d\n*PB = %d\n",a,b,*pa,*pb);
return 0;
}
The output will be:
A = 5
B = 10
A = 50
B = 100
*PA = 50
*PB = 100
As you can see A and B got new values. I hope you understand that.
when you pass things by pointer, if you want to get to the values, you need to do
(*x > *y)
Which gets the value pointed at by the pointer. (x and y are pointers, so they are going to contain memory addresses of the where the values are stored)
Needs to be:
if (*x > *y) {
*z = *x;
}
else if (*y > *x){
*z = *y;
}
You are comparing pointer addresses. You should de-reference the pointers for comparisons and assignments.
void max(int* x, int* y, int* z)
{
if (*x > *y) {
*z = *x;
}
else if (*y > *x){
*z = *y;
}
}

Pointer returning memory address?

I'm working with the program that scans a number in the main program. After that this program calls for a function change_number(), and as an argument gives the numbers memory address. After this program should add 3 to the number in the sub-program, print it out in the subprogram and restore that new value. However, when trying to print the number out in the subprogram change_number(), it prints out it's memory address. My understanding is that a program should return integers value when referring to the pointer with * -notation or just by inserting a variables name. Another compiler which i have tried says the following error message, and does not even compile, either with x -notation or with *pointer_x -notation:
"You are trying to initialize a variable to a value that is of the wrong type.
code.c:20: warning: assignment from incompatible pointer type".
I don't understand because my pointer is introduced as an integer, just like the integer itself. Here is the code:
#include<stdio.h>
void change_number(int *x);
int main()
{
int x;
printf("Give number x: ");
scanf("%d", &x);
printf("In main program: x = %d\n", x);
change_number(&x);
printf("In main program: x = %d\n", x);
return 0;
}
void change_number(int *x)
{
int *pointer_x;
pointer_x = &x;
x = x + 3;
printf("In sub program: x = %d\n", *pointer_x);
}
The code you've pasted should fail to compile on the line pointer_x = &x; as both x and pointer_x are type int*
Using the address-of operator on a pointer variable gives you a pointer-to-pointer - in this case, &x yields a type of int**
In addition, the line x = x + 3 advances a pointer location in memory by 3*sizeof(int) bytes, it's not modifying the original int variable.
Perhaps you intended to write *x = *x + 3 instead?
void change_number(int *x)
{
int *pointer_x;
pointer_x = x;
*x = *x + 3;
printf("In sub program: x = %d\n", *pointer_x);
}
The parameter x already contains address of the variable x from main so it have to be written as
void change_number(int *x)
{
int *pointer_x;
pointer_x = x;
*x = *x + 3;
printf("In sub program: x = %d\n", *pointer_x);
}
When you write void change_number(int *x), x is received as an int *. So, x points to int and *x is int.
So you'll need to change the following:
pointer_x = x;
*x = *x + 3;
printf("In sub program: x = %d\n", *pointer_x);
Now this prints correctly. But to restore the value, just add this line at the end:
*x = *x - 3;

Resources