-3
#include <stdio.h>
void main()
{
    char *s= "hello";
    char *p = s;

    printf("%c\t%c", p[0], s[1]);
}

output of this program is : h e

Can anyone please explain how this program is working? I'm relatively new to c..

Joehl
  • 3,671
  • 3
  • 25
  • 53
lucifer
  • 175
  • 14
  • This is because s is the pointer to the memory location containing "hello" which is allocated due to initialization. Now p is the new pointer pointing to the same memory location, as you pointed *p = s. Now they are pointing to same string in the same memory. Please let me know if it looks ok to you. – kinshuk4 Oct 06 '15 at 10:50
  • @kinshuk4 can you explain in detail?Here p and s are not declared as array.So how we can access the particular element(i.e. p[0],s[1])? – lucifer Oct 06 '15 at 10:55
  • print `p` and `s` as well, with the pointer format `%p`; then you'll see that they contain the same address. – Peter - Reinstate Monica Oct 06 '15 at 10:56
  • 1
    Please Study pointers in C from some book, which you feel comfortable to read. – kinshuk4 Oct 06 '15 at 10:58
  • @kinshuk4 Thanks ..... – lucifer Oct 06 '15 at 11:03
  • @PeterSchneider Thanks... – lucifer Oct 06 '15 at 11:03
  • @kinshuk4 "Study pointers in C from some book, which you feel comfortable to read" -- there you have an oxymoron ;-) – Peter - Reinstate Monica Oct 06 '15 at 11:05
  • 1
    You certainly should read about array and pointer equivalency in C. You might start for example from [Googling 'arrays and pointers in C'](http://www.google.com/?#q=arrays%20and%20pointers%20in%20C). :-) – CiaPan Oct 06 '15 at 11:15
  • While we are at it, just buy Kernighan/Ritchie: The C Programming Language" (https://en.wikipedia.org/wiki/The_C_Programming_Language), and read it, entirely. – Peter - Reinstate Monica Oct 06 '15 at 11:19
  • You do have an array - the string `"hello"` has type `char[6]`. See [What is the type of string literals in C/C++?](http://stackoverflow.com/questions/2245664/what-is-the-type-of-string-literals-in-c-c) – Bo Persson Oct 06 '15 at 12:17

2 Answers2

0

p[0] is identical to *(p+0) , similarly goes for s[1] . [] always operates on a pointer and is same for arrays and pointers.

Note - There is no array declared in your program.

ameyCU
  • 16,489
  • 2
  • 26
  • 41
  • Hmm... no array may be declared, but one is defined and intialized by the string literal. The standard says "The multibyte character sequence [resulting from processing the literal, p.s.] is then used to initialize an array of static storage duration..." – Peter - Reinstate Monica Oct 06 '15 at 11:02
  • @PeterSchneider It will be replaced if suitable to do so . `s` is a pointer to first element to that literal. – ameyCU Oct 06 '15 at 11:06
  • 1
    *'First of all there is no array in your program'*...? First of all you have no idea what you're talking about, Of course there is an array! The string literal `"hello"` defines a static, anonymous, 6-items `char` array. The compiler might 'optimize it out' and not put it in memory if it detects that the array is constant and only two items of it are used, but syntactically it is an array. – CiaPan Oct 06 '15 at 11:10
  • Yeah... or more exact: "s is a pointer to the first element of the array which was initialized with that literal". Strictly spoken, the literal isn't the array proper -- there is some processing like concatenation, replacing of escape sequences, appending zero (and much more when wide chars are involved). The literal is, above all, in the source code, while the resulting array, of course, is in the resulting program or object file. Your remark that neither p nor s are arrays was, by the way, probably hitting exactly the OP's misunderstanding; I was just nitpicking. – Peter - Reinstate Monica Oct 06 '15 at 11:11
  • 1
    @CiaPan You didn't notice that note was edited before your comment . I said it is not declared . It is later replaced and has a type of char array , but that won't be called as declaration ( _would you say `s` is declared as char array ?_ ).but I do agree with what you you try to convey . – ameyCU Oct 06 '15 at 11:24
0

Please note the following facts first( They are neutral to programming LANGUAGE )

  1. Any pointer has/ takes memory equals to size of your systems data bus

  2. even void* takes size equals size of your systems data bus

  3. Now size of data bus is size of processors data fetching/manipulating capacity, you might heard 32 Bit processor, 64 Bit processor

  4. Finally processors data fetching/manipulating capacity equals size of your int, that's why we use following code to calculate Architecture of CPU


#include<stdio.h>
int main(){
if(sizeof(int)==2) {
    printf("\n 16 Bit Architecture, may be using DOS & Turbo C++ IDE");
}else if(sizeof(int)==4)  {
    printf("\n 32 Bit Architecture");
}else {
    printf("\n 64 Bit Architecture");
}
return 0;
}

Showing RAM Status