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.
Related
This question already has answers here:
Undefined, unspecified and implementation-defined behavior
(9 answers)
Closed 4 years ago.
if the variable is initialized (i = 0), it's still 1 each time the function func is called, BUT
when i is not initialized:
#include <stdio.h>
int funct(void);
int main(void)
{
funct();
funct();
funct();
return 0;
}
int funct(void)
{
int i;
static int j = 0;
i++;
j++;
printf(" i = %d j = %d\n", i, j);
}
the output is
i = 1 j = 1
i = 2 j = 2
i = 3 j = 3
I don't understand why the variable i behaves like a static one!
The value is unspecified, so anything goes. But, likely the same memory is reused for each call to funct and with that, the same memory is reused and the i just pick up the old value left from the previous run.
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;
}
This question already has answers here:
Why can't I increment an array?
(5 answers)
Closed 5 years ago.
This code works fine when b is incremented and a is printed upon increment
#include<stdio.h>
int main()
{
int a[] = {10,20,30,40,50}, *p, j;
int *b = a;
for(j=0;j<5;j++)
{
printf("%d\n",*b);
b++;
}
return 0;
}
What happens here? What effect does a++ have here to suggest lvalue is required. Does a++ move to a point after all the elements of the array a?
#include<stdio.h>
int main()
{
int a[] = {10,20,30,40,50}, *p, j;
for(j=0;j<5;j++)
{
printf("%d\n",*a);
a++;
}
return 0;
}
a is an array which decays into a (unmodifiable) pointer to the array (and only and always to the array) when used in pointer context. So it cannot be modified, so a++ doesn't work.
b is a pointer which can point everywhere and thus as well can be modified, so b++ works.
This question already has answers here:
Changing address contained by pointer using function
(5 answers)
Closed 4 years ago.
#include<stdio.h>
void foo(int*);
int main()
{
int i = 97, *p = &i;
foo(p);
printf("%d ", *p);
getch();
}
void foo(int *p)
{
int j = 2;
p = &j;
printf("%d ", *p);
}
Output is 2 97
Why not 2 2 ?
pointer p holds the address of j now so why 97 is printed ?
You can imagine the function call and its definition the following way. For clarity I'll rename the function parameter as q.
foo(p);
void foo( /*int *q*/ )
{
int *q = p;
int j = 2;
q = &j;
printf("%d ", *q);
}
As it is seen the function parameter (in this case q) is a local variable of the function that is initialized by the value of an argument (in this case by the value of the argument p)
So any changes of the local variable (q) do not influence on the original argument (p).
After exiting the function the local variable will not be alive.
If you want to change the argument itself you should pass it by reference. For example
void foo( int **p )
{
int j = 2;
*p = &j;
printf("%d ", **p);
}
However after exiting the function the original argument/variable p will be invalid because it stores the address of a non-alive local variable of the function j.
So an attempt to access the memory that was occupied by the function's local variable j results in undefined behavior of the program.
You could make the program correct by declaring the local variable j as having static storage duration. For example
void foo( int **p )
{
static int j = 2;
*p = &j;
printf("%d ", **p);
}
and call the function like
foo(&p);
In foo, you assign a new value to p. However this is a copy of the value of p in main, so the change is not visible in main.
If you dereferenced p, then it would change the value if i in main:
void foo(int *p)
{
*p = 2;
printf("%d ", *p);
}
p = &j;
just changes the value of the local variable p. This has no effect on the caller's p variable (because p was passed by value) or the variable that p previously pointed to (because you didn't indirect through it). If you want to change the caller's data, write:
*p = j;
Go through the above code line by line. Here I have written comments as control passes through each of the lines...
#include<stdio.h>
void foo(int*);
int main()
{
int i = 97, *p = &i; //Lets assume i has address 2000
foo(p); //p contains 2000 which is passed to foo. go to foo at 1.
printf("%d ", *p); // when control comes back no change to p it
//still points to 2000 which stores 97
getch();
}
void foo(int *p) 1: // in foo another local variable p is created.
//Let's call this lp. lp has now address 2000. i.e lp
//and p both point to i but one locally exists in a
// function and will be destroyed when control comes out
// of the function
{
int j = 2;
p = &j; // now local p i.e lp points to another var j
// address. Suppose lp has address 3000 now.
printf("%d ", *p); //val at address in lp is 2
}
This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
How to access a local variable from a different function using pointers?
(10 answers)
Closed 8 years ago.
int*func();
int main()
{
int i;
int *ptr;
ptr=func();
for(i=0;i<10;i++)
{
printf("%d ",*ptr);
ptr++;
}
}
int* func()
{
int arr[10];
int i;
for( i=0;i<10;i++)
arr[i]=i+1;
return arr;
}
Why i am not getting my output as 1,2,3,4,5,6,7,8,9,10??
I am returning the address of the array from func() but
i am still getting junk values.
Wow - aggressive downvoting! Give the new person a chance folks!
The answer is "scope". If you compile with gcc, you get a very obvious warning:
C:\tmp>gcc test.c
test.c: In function `func':
test.c:22: warning: function returns address of local variable
The local variable is lost when the function exits and its memory allocation cleaned up.
If you want to return an array from a function you have to malloc it to create a non-local memory allocation and then return the pointer.
You're returning a pointer to a local variable, which results in undefined behaviour.
The most common way to deal with functions that return arrays is to pass in the array from the caller, e.g. fixed version of your code:
void func(int arr[]);
int main()
{
int arr[10];
func(arr);
for (int i = 0; i < 10; i++)
printf("%d ", arr[i]);
return 0;
}
void func(int arr[])
{
for (int i = 0; i < 10; i++)
arr[i] = i + 1;
}