0

Possible Duplicate:
What is the difference between char s[] and char *s in C?

char *p="Help"
printf("%ud",p);

i get the base address of "Help" as output.Does that mean that a string("Help")always returns its base address.If so Is this the same case with C++.

also can u explain what exactly is happening below.

char name[]="Hello";

I know this is silly but my brain is not in peace.

Community
  • 1
  • 1
tez
  • 4,990
  • 12
  • 47
  • 67

4 Answers4

1

In the first example you give in your post you are using a pointer which points to an address in memory where the characters are stored. That's why when you print p you get an address.

In your second example you are creating an array of characters each storing a letter in "Hello". An array uses some similar memory access principles in that each character is stored in sequential memory locations so name[] is a memory location and then any index you access is an offset in memory to the first pointer name[] where the first element lives.

Say, for simplicity, that name[0] = 0x00, name[1] = 0x01, name[2] = 0x02 and so on. Now, each of those memory locations holds a value which represents part of your string "Hello". When accessed, it is known where name[x] is by which index you are accessing and you get that memory location or character depending on how you access the element.

NKamrath
  • 536
  • 3
  • 9
0

If you want to print the string do this:

char *p="Help"
printf("%s",p);

The line

char name[]="Hello";

declares name as an array of char and initializes it to contain the literal "Hello" (five ASCII characters terminated with 0, or NULL).

piokuc
  • 25,594
  • 11
  • 72
  • 102
0

A string in C is an array of charaters in memory (terminated with the null character). The variable p in your example is a pointer to that memory address, which is what you print out.

The second example is the same, except you declare the array on the stack (as opposed to the compiler's chosing).

The main difference is that using the array ([]) notation, you can easily calculate the size of the array (and hence the string), but using the pointer notation you need to traverse the array to find the end to calculate the size.

It is similar in C++, but you are better off using the std::string type as that allows you dynamic memory management (e.g. when concatenating strings)

Attila
  • 28,265
  • 3
  • 46
  • 55
0

When you use %u in printf it always prints the value of any variable. And you are using string as a variable. In strings, the address of the first character and base address of the string is same. Hence it will always display the base address of the string. For example,

char *p="Help";
printf("%u",p);

will always disply the base address.

And regarding your second question you must know the concept of implicit and explicit initialization. When you manually give the size of character array it is called as explicit initilization. e.g. char str[10]="abcdef";

And in other case like char str[]="abcd";, it implicitly takes the length of the string as the size of the character array (in this case 4, starting with 0 to 3 and end as null character '\0'). I hope you understood this.

Mike Weller
  • 45,401
  • 15
  • 131
  • 151
NappY
  • 1