Accounting for pointer initialization

We could initialize a character pointer like this in C.

char *c="test";

Where c indicates the first character (t).

But when I gave the code as below. It gives a segmentation error.

 #include<stdio.h> #include<stdlib.h> main() { int *i=0; printf("%d",*i); } 

Also when i give

 #include<stdio.h> #include<stdlib.h> main() { int *i; i=(int *)malloc(2); *i=0; printf("%d",*i); } 

It worked (gave a result of 0).

When I gave malloc(0) , it worked (gave the result 0).

Tell me what is going on

+4
source share
1 answer

Your first example is seg faulting, because you are trying to remove the reference to the null pointer that you created with the line:

 int *i=0; 

You cannot turn off a pointer that does not point to anything and expect something to happen. =)

The second code segment works because you actually assigned memory to your pointer using malloc, which you can remove. I think it's possible for you to get non-zero values, depending on the memory adjacent to the address allocated with malloc. I say this because, as a rule, int is 4 bytes, and you have assigned only 2. When unlinking an int pointer, it should return an int value based on the specified 4 bytes. In your case, the first 2 bytes - this is what you received from malloc and adjacent 2 bytes - this is all that can be anything and whatever it is, it will be processed as if it were an int. You may get strange behavior like this, and you should malloc the size of memory needed for the type you are trying to use / specify.
(i.e. int *i = (int *) malloc(sizeof(int)); )

Once your pointer points to memory with the correct size, you can set the values ​​as such:

 #include <stdlib.h> #include <stdio.h> int main (int argc, char *argv[]) { int *i = (int *)malloc(sizeof(int)); *i = 25; printf("i = %d\n",*i); *i = 12; printf("i = %d\n",*i); return 0; } 

Edit based on comment:

A pointer points to memory, not values. When initialized, char *ptr="test"; You do not assign the value "test", you assign the memory address where the compiler places the "test", which is placed in the process data segment and is read-only. You tried to change the string "test", you most likely ran the seg error. What you need to know about char * is that it points to one (i.e. the first) character in a string. When you cancel the link to char *, you will see only 1 character and one character. C uses null-terminated strings and notices that you are not de-referencing ptr when you call printf, you pass the pointer to it and only point to the first character. How this is displayed depends on the format passed to printf. When printf is transmitted in the format "% c", it will print dots with one ptr character, if you pass the format "% p", it will print the address that ptr indicates. To get the whole line, you pass "% s" as the format. What printf do does is start with the pointer you passed in and read every subsequent byte until null is reached. Below is the code demonstrating them.

 #include <stdlib.h> #include <stdio.h> #include <string.h> int main (int argc, char *argv[]) { // Initialize to data segement/read only string char *ptr = "test"; printf("ptr points at = %p\n", ptr); // Prints the address ptr points to printf("ptr dereferenced = %c\n", *ptr); // Prints the value at address ptr printf("ptr value = %s\n", ptr); // Prints the string of chars pointed to by ptr // Uncomment this to see bad behavior! // ptr[1] = 'E'; // SEG FAULT -> Attempting to modify read-only memory printf("--------------------\n"); // Use memory you have allocated explicitly and can modify ptr = malloc(10); strncpy(ptr, "foo", 10); printf("ptr now points at = %p\n", ptr); // Prints the address ptr points to printf("ptr dereferenced = %c\n", *ptr); // Prints the value at address ptr printf("ptr value = %s\n", ptr); // Prints the string of chars pointed to by ptr ptr[1] = 'F'; // Change the second char in string to F printf("ptr value (mod) = %s\n", ptr); return 0; } 
+5
source

Source: https://habr.com/ru/post/1334456/


All Articles