a simple pointer code - c

#include <stdio.h>
int main(void)
{
int* a;
*a=20;
printf("%i\n",*a);
return 0;
}
I have the code above. when the code in runtime, I always get the error message "filename.exe has stop working". Why?

You did not allocate any memory for the pointer to point at. You can do so like this:
int *a = malloc(sizeof(*a));
or like this:
int value;
int *a = &value;
If you allocate with malloc then you'll want to call free on the pointer when you are finished using it.
Accessing an uninitialized pointer leads to undefined behaviour. In your program it led to segmentation fault, one very common outcome of uninitialized pointer access.

In int* a; a's default value is garbage, and points to an invalid memory, you can't assign to that. And assignment like *a=20; this is causes an undefined behavior at run time. (syntax wise code is correct so compiled) you may some time get a seg-fault too.
either do:
int i;
int *a = &i; // a points to a valid memory that is i
*a = 20;
or with dynamic memory allocation using calloc() or malloc() functions.
int *a = malloc(sizeof(int));
*a = 20;
Remember dynamic allocated memories we have to deallocate (free) explicitly when we have done with that.

You have wild pointer, either assign memory to it using malloc
int* a = malloc(sizeof(int));
or use a stack variable
int b = 0;
int *a = &b;
*a=20;

The problem is in your assignment
*a = 20.
You can't allocate a value to a pointer variable like that.
int b = 20;
a = &b;
Thanks,
Santhosh

Related

Should every pointer in C be allocated on the heap?

Is it required, or recommended, that every pointer in C be allocated on the heap? For example, is it likely or possible that the following code could produce a segmentation fault?
#include <stdio.h>
int main(void) {
int* p;
*p = 16;
printf("Pointer p = %d\n", *p);
return 0;
}
This code will likely segfault because p is uninitialized and therefore doesn't point to a valid address.
A pointer doesn't necessarily have to point to heap memory. It just needs to point to a valid object. For example:
int x = 4;
int *p = &x;
*p = 5;
printf("x=%d\n", x); // prints 5
It doesn't have to be allocated. You just need to make sure it points to valid memory address, which you didn't in your example.
Since you didn't initialize the pointer, it has some random value and trying to dereference it will interpret this value as a memory address, which is probably going to give you a segmentation fault.
You could instead do something like:
int x; // not initialized, random value in memory
int *p = &x;
*p = 16; //now you are accessing a valid address (x's)

Passing double pointer as function argument

I simply want to assign a pointer to another pointer via the function (same memory address). My code is below:
#include <stdio.h>
void d(int** a)
{
int* val_ptr = malloc(1);
*val_ptr = 5;
printf("%d\n", *val_ptr);
a = &val_ptr;
}
int main()
{
int* a = NULL;
d(&a);
printf("%d\n", *a);
return 0;
}
Output from Link
5
Segmentation fault
Your code has three problems:
Here int* val_ptr = malloc(1);, you allocate 1 byte rather than allocating space for an int. Use the following to fix it:
int* val_ptr = malloc(1 * sizeof(int));
This a = &val_ptr; is not what you want. It changes the local pointer and makes it point to the address of val_ptr. This will not affect the pointer that you've defined in main.
Fix it using
*a = val_ptr;
This way, the pointer in main will also reflect the change and will point to the malloced memory
You should free the allocated memory after its use. Add
free(a);
after the printf in main to free it.

Return pointer from a function without defining the variable as static?

#include <stdio.h>
#include <stdlib.h>
int* func();
int main(void) {
int *b = NULL,i;
b = func();
for(i=0;i<7;i++)
{
printf("%d\n",*b);
b++;
}
}
int * func()
{
int *p;
p = malloc(sizeof(int) * 7);
int arr[]={1,2,3,4,5,6,7}; //without using static
p = arr;
return p;
}
How to return the address of the array to the main function for printing the values ?? Without declaring the array as static if we allocate memory for the array in heap then can we able to pass the array to the function as a pointer ??
You are close, but not quite there.
The following expressing causes p to point to arr's address which is not the intended effect:
p = arr;
Remember, p is a pointer, and if you do not use the dereference operator *, then you are refering to the pointer's address rather than its value. arr's memory is deallocated when the function exits, and the memory malloc'd to p is lost because you reassigned the address of p to point to the address of the local variable arr.
The solution is to copy the values of arr to p using a for loop:
int i = 0;
for (; i < 7; ++i) {
p[i] = arr[i];
}
This will print the desired result because you replaced the values of what p pointed to rather than the address of p itself.
You can't. The array is gone when you leave the function, any pointer to the array would have an undeterminate value when you leave the function, and using such a pointer in any way invokes undefined behaviour.
Your malloc () call is pointless and leads to a memory leak, because you allocate space for 7 ints on the heap, and then overwrite the pointer, so the memory can never be used or freed.

Why do these pointers cause a crash?

I'm a bit confused as to why the following code crashes:
int main(){
int *a;
int *b;
*a = -2;
*b = 5; //This line causes a crash on my system.
return 0;
}
Shouldn't memory automatically be allocated for two pointers and two integers before run-time because of the declarations?
Or must you always explicitly allocate memory?
No. You've only declared the pointers, not what they point to. The pointers are allocated on the stack, and since you've not initialized them to anything, their values are garbage.
int main() {
int a = 7;
int *p_a; // p_a contains whatever garbage was on the stack at its
// location when main() is called. (Effectively points nowhere).
p_a = &a; // p_a points to (gets the address of) variable a, also on
// the stack.
printf("Before: a = %d\n", a); // prints 7
*p_a = -2;
printf("After: a = %d\n", a); // prints -2
return 0;
}
I would code up the above example, and step through it in a debugger. You'll see what I mean about what p_a is pointing to.
Shouldn't memory automatically be allocated for two pointers and two integers before run-time because of the declarations?
I only see you specifying two pointers. Where are the two integers?
Or must you always explicitly allocate memory?
Pointers have to point to something. Either local variables on the stack, or malloc'd memory from the heap.
In this code:
int* a;
*a = -2;
a is an uninitialized pointer, dereferencing of which produces undefined behavior, that you were luckily able to observe as a crash of your application.
You need to initialize the pointer (make it point to the valid memory) before you dereference it (i.e. before you use *, the dereference operator):
int a;
int* pA = &a;
*pA = -2;
Consider
int m;
int n;
m = n;
This is invalid because you're trying to use n but you haven't assigned a value to it. Now:
int *a;
*a = -2;
Likewise, this is invalid because you're trying to use a but you haven't assigned a value to it. The value of a is not an int, it's a pointer to int. For example,
int someint;
a = &someint;
*a = -2;
puts -2 into someint. Without the assignment to a, the place to put -2 is undeterminable. Also,
a = malloc(sizeof(int));
*a = -2;
Here, a is given the value of the address of some location in the heap; -2 goes into that heap location.
Perhaps an analogy would be helpful:
Consider the phrase "her dog". This is a reference to someone's' dog, but it won't do to tell me "give her dog a bone" if you haven't told me who she is. Similarly, "pointer to an int" doesn't tell the system which int it is.
Your *a and *b pointers are not initializated properly.
Try this one:
int my_a;
int my_b;
int *a;
int *b;
a = &my_a; // init the pointer a to the direction of my_a int variable
b = &my_b;
*a = 3; // set the my_a value via pointer
*b = 2;
You have just declared pointers but you haven't initialized them. So, you can't be sure that *b = 5 is causing the program to crash. It could be *a = -2 as well. To fix it, you should initialize your pointers as well.
int aval = -2;
int bval = 5;
int *a = &aval; // declared and initialized pointers
int *b = &bval;
// Now you can change the value using the pointer
*a = 15;
*b = 20;

Can't assign values to variable and pointer [duplicate]

This question already has answers here:
Segmentation Fault when attempting to print value in C
(3 answers)
Closed 3 years ago.
I'm very new to C, but have no idea why this program breaks. The program compiles and runs if I remove the lines that have to do with i, but if I assign i, I can no longer assign anything to *ptr without the program breaking.
int main(void)
{
int i;
int *ptr;
i = 2;
*ptr = 5;
printf("%d",*ptr);
}
You leave the pointer with uninitialized value. So when you dereference it (*ptr), you access arbitrary place in memory, resulting in a segmentation fault.
Point ptr at something by assigning to ptr itself (not *ptr) an address of a variable (like &i) or some freshly allocated memory (like malloc(sizeof(int))).
Here is the answer for C:
int main(void) {
int i;
int * ptr = (int *)malloc(sizeof(int));
i = 2;
*ptr = 5;
printfn("%d",*ptr);
free(ptr);
}
Alternatively you could for the i and *ptr assignment lines use something like:
int main(void) {
int i;
int * ptr;
i = 2;
ptr = &i;
printfn("%d",*ptr); // should print 2
}
Notice also that the free came out!!!
You declared ptr but didn't make it point to anything. Then you tried to write to what it points to. This is never a good idea. Try making ptr point to i by adding the line
ptr = &i;
before you try to write to *ptr
Before using a pointer in C, you need either to set the pointer to an existing block of memory, you need to allocate memory for it, like this.
int *ptr = (int *)malloc(sizeof(int));

Resources