This question already has answers here:
Does C have references?
(2 answers)
Closed 4 years ago.
I'm learning referencing in C and here is my code:
#include <stdio.h>
int main(void)
{
int x = 5;
int &ref = x;
printf("%d\n", x);
printf("%p\n", &x);
printf("%p\n", &ref);
return 0;
}
When i compile, it shows these errors:
reference.c:7:6: error: expected identifier or '('
int &ref = x;
reference.c:11:18: error: use of undeclared identifier 'ref'
printf("%p\n", &ref);
2 errors generated.
What should i do to fix this and thanks in advance.
c does not have a reference type, you can only use point instead of ref.
#include <stdio.h>
int main(void) {
int x = 5;
int *ref = &x;
printf("%d\n", x);
printf("%p\n", &x);
printf("%p\n", &ref);
return 0;
}
A "reference" in C is called a pointer. Through the pointer you reference something. In your code you need to write:
#include <stdio.h>
int main(void)
{
int x = 5;
int *ref = &x; // now ref points to x
printf("%d\n", x); // print the value of x
printf("%p\n", &x); // print the address of x
printf("%p\n", &ref); // print the address of the pointer variable
printf("%d\n", *ref); // print the value of the int that ref is pointing to
return 0;
}
Related
I have written a program that defines 3 int variables, and printed them out using %p format specifier.
The output is as follows:
0000000000000001
0000000000000002
0000000000000003
My question is now how do I figure out where in memory these variables are stored. Since I'm using the %p format specifier, are the outputs already the addresses in memory where variables are stored?
Here is my code:
int main(void)
{
int iX1 = 1;
int iX2 = 2;
int iX3 = 3;
printf("%p\n", iX1);
printf("%p\n", iX2);
printf("%p\n", iX3);
return EXIT_SUCCESS;
}
%p is only the format specifier for the data you provide.
To get the address of a variable use the & operator like this:
#include <stdio.h> // printf
#include <stdlib.h> // EXIT_SUCCESS
int main(void)
{
int iX1 = 1;
int iX2 = 2;
int iX3 = 3;
printf("%p\n", (void*){&iX1});
printf("%p\n", (void*){&iX2});
printf("%p\n", (void*){&iX3});
return EXIT_SUCCESS;
}
Output:
0x7ffee4f7e8e8
0x7ffee4f7e8e4
0x7ffee4f7e8e0
I have a function which returns an integer pointer type:
int* f(int a, int b){
int *result;
result = &a;
*result += b;
return result;
}
and when I call this on main:
int main(){
int a = 5;
int b = 2;
int *result = f(a,b);
printf("The result is: %d \n", *result);
return 0;
}
It gives me the correct output(in this case 7). I was under the impression that by assigning the address of the parameter a to result I would get a segmentation fault when I ran this function.
My assumption is that C treats function parameters as local in scope to the function definition. But, I see that this is not the case so why is this specific program working ?
I'm using Code::Blocks 16.01 with gcc compiler.
Just because it works on your machine doesn't mean it isn't undefined behaviour. This works by fluke, but it's invalid.
It may produce the correct result because that stack is not overwitten or otherwise mangled by the time you do something later on.
For example, if you make another function call:
#include <stdio.h>
#include <stdlib.h>
int noop(int x, int y) {
return x + y;
}
int* f(int a, int b){
int *result;
result = &a;
*result += b;
return result;
}
int main(){
int a = 5;
int b = 2;
// Do something with undefined behaviour
int *result = f(a,b);
// Do something else which uses the stack and/or the same memory
int x = 10;
int y = 11;
int z = noop(x, y);
printf("The result is: %d \n", *result);
return 0;
}
Now the output gets stomped with the definition of x which coincidentally takes the same piece of memory so the output is 10. As this is undefined behaviour, though, anything could happen, including a crash.
This question already has answers here:
Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized pointer
(5 answers)
Closed 4 years ago.
I solved this simple question in C on my lab's system (which uses Windows). I used DevC++ to write this code and it ran on DevC++, but I cannot compile the same code on my Mac system.
Code:
#include<stdio.h>
#include<stdlib.h>
int* oddSwap(int x, int y)
{
int temp = x;
x = y * 5;
y = temp * 5;
int *k;
*k=x+y;
return k;
}
int main()
{
int x=5;
int y=3;
int *k=oddSwap(x,y);
printf("\n%d", *k);
return 0;
}
This ran on Windows (DevC++), but I'm getting this error message on running it on macOS (macOS 10.14.2).
Bus error: 10
The swap-function does not make much sense, since you swap the arguments only within the function, but the result then combines both values with commutative operator + (so why swap at all?).
And you assign a value to a pointer that does not point to a valid object: int *k; *k=x+y;. But anyway, the complete operation does not make sense to me.
Maybe you wanted something like that:
void oddSwap(int *x, int *y) {
int temp = *x;
*x = *y * 5;
*y = temp * 5;
}
int main()
{
int x=5;
int y=3;
oddSwap(&x,&y);
printf("\n%d:%d", x,y);
return 0;
}
There is an error in your code.
int* oddSwap(int x, int y)
{
int temp = x;
x = y * 5;
y = temp * 5;
int *k; // <-- the pointer 'k' is never given a value, yet you de-reference it below.
*k=x+y; // <-- this operation is invalid.
return k;
}
When you do *k = <something> you dereference the pointer.
That is, you assign <something> to the variable that k points to.
For that to be valid, you need to give k a value, i.e. make it point to something.
This question already has answers here:
returning a local variable from function in C [duplicate]
(4 answers)
Closed 7 years ago.
Why is the First Printing Statement in main(), printing 11 ?
#include<stdio.h>
void foo(int ** p){
int j = 11;
*p = &j;
printf("%d ", **p); //Printing 11
}
int main(){
int i = 10;
int *p = &i;
foo(&p);
printf("%d ", *p); //Printing 11
printf("%d ", *p); //Printing Random value
return 0;
}
Inside foo() , you're assigning the address of a automatic local variable j to *p. After foo() has finished execution, j does not exist anymore and thus, using (derererencing) p furthermore in main() invokes undefined behavior.
Now, the output of UB is, well, undefined.
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;