简体   繁体   中英

Pointers to strings

I am a newbie to C programming. And I am confused with the chaotic behavior of pointers. Specially when it comes to strings and arrays.

I know that I can't write like,

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

Because clearly it will try to write to the 5th location of the memory.And that will crash the program.I have to initialize the "number".

But when it comes to strings I can write like,

#include <stdio.h>
int main()
{
 char *name="xxxxx";
 printf(name);
} 

And it works too. So that means it implicitly initialize the "name" pointer.I also know that name=&name[0] But I have found that name=&name too. How can it be? Because, to me it looks two variables with the same name. Can anybody tell me how strings are created in memory?(All this time time I assumed it creates name[0].....name[n-1] and another variable(a pointer) called "name", inside that we put the location of name[0].Seem to be I was wrong.)

PS:-My English may not be good and if somebody can give me a link regarding above matter that would be grateful.

C, like many programming languages, supports the concept of "literals" - special syntax that, when encountered, causes the compiler to create a value in a special way.

-2 , for example, is an integer literal . When encountered, the compiler will treat it as a value of type int with the content of -2. "..." is a string literal - when encountered, the compiler allocates new space in a special memory area, fills it with data that corresponds to the chars you used inside the literal, adds a 0 at the end of that area, and finally uses a pointer to that area, of type char* , as the result of the literal expression. So char* s = "hello" is an assignment from something of type char* into a variable of type char* - completely legal.

You sneaked in another question here - why a == &a[0] . It's best to ask one question at a time, but the gist of it is that a[n] is identical to *((a)+(n)) , and so:

&a[0] == &*(a+0) == a+0 == a
 char *name="xxxxx";

This creates a char array (const) in memory, and it will assign the address of 1st element of it to name . char* array names are like pointers. &name[0] means address of 1st element and name (In c,cpp just the char * array name will provide you with the address of 1st element too (bcos that is what was assigned to name in the 1st place itself)) also gives the same result.

The notation name[i] is translated as *(name+i) so u actually have a base address name to which you add subscripts. (calculations are as per pointer arithmetic) . printf("%s", name) is designed to print from start address to \\0 (which is appended to the end of strings created using char* aka String literals)

Check this too.

That is because you those strings are actually arrays of characters. What you do with the line char *name = "xxxxx"; : You construct an array of 6 character values (5 of them being 'x' and the last one being '\\0' ). name is a pointer to that array.

That's how strings are normally handles in C, you have some kind of sequence of characters, terminated with a '\\0' character, to tell functions like printf where to stop processing the string.

Let's consider this piece of code:

 char *name="xxxxx";

What happens here is that the string xxxxx is allocated memory and a pointer to that memory location,or the address of that string is passed to the pointer variable name .Or in other words you initialize name with that address.

And printf() is a variadic function (one that takes one fixed argument followed by a random number of arguments).The first argument to printf() is of type const char* .And the string identifier name when passed as an argument denotes the base address of that string.Hence you can use

printf(name);

It will simply output xxxxx

name=&name too. How can it be? --Well that's a justified question.

Let me explain first with a real-world analogy. Suppose you have the first house in a row of houses in a housing society.Suppose it's plot number 0 .The other houses are on plots 1,2,3......Now suppose there is a pointer to your house , and there is another pointer to the whole housing society's row . Won't the two pointers have the same address, which will be plot 0 ?This is because a pointer signifies a single memory location.It's the type of the pointer that matters here.

Bringing this analogy to the string( array of characters), the name identifier only signifies the base address of the string, the address of its first character (Like the address of the first house).It is numerically same to the address of the whole string,which is (&name) ,which in my analogy is the ROW of houses.But they are of different types, one is of type char* and the other is of type char** .

Basicly what happens when the C-compiler see the expression

char *name = "xxxxx";

is, it will say. Hey "xxxxx" that's a constant string (which is an array of bytes terminated with a 0 byte), and put that in the resulting programs binary. Then it will substitute the string for the memory location, sort of like:

char *name = _some_secret_name_the_compiler_only_know;

where _some_secret_name_the_compiler_only_know is a pointer to the memory location where the string will live once the program gets executed. And get in with parsing the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM