C char pointers

Say we have an array of pointers:

char *ptr[30];

Now it works great and it seems nothing unexpected! I can easily enter names.

 scanf("%s", ptr[1]); scanf("%s", ptr[2]); scanf("%s", ptr[3]); printf("%s\n", ptr[1]); printf("%s\n", ptr[2]); printf("%s\n", ptr[3]); 

My question is, can a pointer be used this way to store a finite number of names, then why is malloc used.? and in this case ptr [1] does not indicate a character at the input, but a new input. for example, if ptr has mukul, ptr [1] must point to 'u' and if space is not allocated when the pointer is declared in this way, what are the limits.?

+4
source share
4 answers

A pointer cannot be used in this way. It may "work" for you sometimes by chance, but using a pointer without allocating memory for it will cause undefined behavior - this means that your program can behave unpredictably and give unexpected results.

Remember that just because compiling and running your program does not mean that it is correct. There is a whole class of errors in C, known as "w91> behavior," many of which the compiler will gladly allow you to complain without. These include overwriting a buffer, using an uninitialized variable, or dereferencing a pointer that does not indicate a legally allocated memory block. Programs that exhibit undefined behavior may even seem to work normally sometimes - but they are usually very unstable and prone to failure.

+13
source

If you use what is in your example, you just write down the other locations that appear after your ptr array. Most compilers should give at least a warning. Your program will break into most systems, you are just very lucky.

+4
source

When you define a pointer like:

 char *ptr = 0; // NULL pointer: dereferencing it will crash puts(ptr); // crash 

You simply create a link to a place in memory:

 ptr = "string"; // dereferencing it will show the string puts(ptr); // displaying "string" 

Thus, having an array of pointers simply creates a list of references to other variables.

To reference a place in memory, you need to assign variables to your pointers or allocate memory for each pointer.

+3
source

You allocated a place for 30 pointers, but you did not initialize them to point anywhere. If you did not declare the array outside the function, each element in the array will contain some random bit string, which may or may not correspond to a writable memory cell. If we drew a picture, it would look something like this (all addresses are out of thin air, don’t assume that this corresponds to any real architecture):

  Item Address 0x00 0x01 0x02 0x03
 ---- ------- ---- ---- ---- ----
 ptr 0xbbc81230 0x ??  0x ??  0x ??  0x ??
            0xbbc81234 0x ??  0x ??  0x ??  0x ??
            0xbbc81238 0x ??  0x ??  0x ??  0x ??
            ...
            0xbbc812a8 0x ??  0x ??  0x ??  0x ??           

where is 0x?? is a random byte value. For the described behavior, each of the random values ​​simply indicates a writable memory, and recording on top of what is stored there simply does not have any immediate side effects.

Bad juju: it looks like your code is working correctly when in fact it behaves very badly and can lead to some unpleasant run-time problems elsewhere in your program, which is a pain to debug.

You will need to explicitly set each element of the ptr array to indicate the correct memory location before attempting to register it.

Suppose we add the following code:

 ptr[0] = malloc(strlen("foo") + 1); strcpy(ptr[0], "foo"); ptr[1] = malloc(strlen("bar") + 1); strcpy(ptr[1], "bar"); 

We dynamically allocated extra memory to hold several lines and saved pointers to these new buffers up to ptr[0] and ptr[1] .

Now our picture will look something like this:

  Item Address 0x00 0x01 0x02 0x03
 ---- ------- ---- ---- ---- ----
            0x80ff0000 'f' 'o' 'o' 0x00
            ...
            0x80ffcdc0 'b' 'a' 'r' 0x00
            ...
 ptr 0xbbc81230 0x80 0xff 0x00 0x00
            0xbbc81234 0x80 0xff 0xcd 0xc0
            0xbbc81238 0x ??  0x ??  0x ??  0x ??
            ...
            0xbbc812a8 0x ??  0x ??  0x ??  0x ??           

ptr[0] now contains the address of the buffer, whose size is 4 char , and we copy the string "foo" into this buffer. Similarly, ptr[1] contains the address of another 4-byte buffer, which now contains the string "bar".

+3
source

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


All Articles