1

I'm new to c language. This is my code

char c[]="name";
char *p="city";
printf("\n 1. memory location of array in pointer is %u",p);
p=c;
printf("\n 2. memory location of array in pointer is %u",p);

it gives me output :

memory location of array in pointer is 177

memory location of array in pointer is 65518

now checkout the difference in memory allocation, when first time

char *p="city"

address in p is 177 and second time when

p=&c;

address in p is 65518. why? I didn't get the address allocation to array. Normally when declare some variable in c, there address is something like 655... and at the time char *p, its different. Is there any specific reason for this.

I'm working on windows 7 32 bit operating system

My question is when

char *p="city"

address in p is 177. why?

Pankaj
  • 33
  • 1
  • 7
  • 1
    First use `%p` to print addresses, Second `p = &c;` is not correct assignment, you are ignoring warning..*`I didn't get the address allocation to array.`* yes but you are assigning different address to `p`. – Grijesh Chauhan Feb 08 '14 at 08:11
  • Read [Difference between `char *str` and `char str[]` and how both are stored in memory](http://stackoverflow.com/questions/15177420/what-does-sizeofarray-return/15177499#15177499) – Grijesh Chauhan Feb 08 '14 at 08:12
  • @Girish :I got that difference, what I want is why at the time of char *p="city", address allocation is 177, I mean this is very short value as compare to other varialbes address. Normally address allocation is something like 655... – Pankaj Feb 08 '14 at 08:19
  • Your `177` and `655` make no sense whatsoever as long as you are using `%u` to print pointer values. `%u` is for `unsigned int` values. Use `%p` to print pointers. – AnT stands with Russia Feb 08 '14 at 17:51

2 Answers2

2

Because the array name decays into pointer to first element when its assigned or passed (assigned to a variable of first element's address type).

p = c;  // & is not needed, and not defined behavior 

printf("%p\n", (void *)&p);

Gives you the address

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • char *p="city"; printf(“%p\n“, (void *)p); is giving me "00B1" – Pankaj Feb 08 '14 at 08:25
  • thanx alter...now it works...but i'm having one more doubt, suppose there is a variable in c say int i, now "i" is equivalent to *(&i) but what is the use of (void *)&p..why void? – Pankaj Feb 08 '14 at 08:48
  • 1
    The type-cast is needed because `%p` (right format for print addresses) expects a void pointer – David Ranieri Feb 08 '14 at 09:00
0

there are different area in memory. when you decalre a varaible it will declare in HEAP are . ROM are where the containt of varible after declare ,cant change. in your question char [], a array means varible it will go in HEAP.so 65432 or something. 2. char *p="name",is a string constant .the space for "name" will be declared in ROM area.so 772.you cant change "name" to "nass" .it may be or may not be. LAST.space for each area given by virtual memory system . ROM .LESS space --less bit addr HEAP- LARGE SPACE so---large bit for addr(generally 4 byte in window 7).