Anusha Pachunuri
Anusha Pachunuri

Reputation: 1419

c char arrays and pointers

#include<stdio.h>
int main(){

char a[6],*p;

a[0]='a';
a[1]='b';
a[2]='c';
a[3]='4';
a[4]='e';
a[5]='p';
a[6]='f';
a[7]='e';
printf("%s\n",a);
printf("printing address of each array element");
p=a;

printf("%u\n",&p[0]);
printf("%u\n",p+1);
printf("%u\n",a+2);
return 0;
}

The output is as follows...

anusha@anusha-laptop:~/Desktop/prep$ ./a.out
abc4epfe
printing address of each array element3216565606
3216565607
3216565608

When I declared the array as char a[6] why is it allowing me to allocate a value at a[7]? Does it not need a null character to be appended for the last element?

Also p=a => p holds the address of first element of char array a. I don’t understand how it is correct to place an '&' in front of an address (p[0]). &p[0] means address of address of first element of a which doesn't make any sense, at least to me.

Why is it printing the correct output?

Upvotes: 2

Views: 1882

Answers (6)

David Schwartz
David Schwartz

Reputation: 182769

When I declared the array as char a[6] why is it allowing me to allocate a value at a[7]?

Because you told it to do that. You're the boss. If you tell it to jump off a cliff, it might.

Does it not need a null character to be appended for the last element?

It's not a string, it's an array of characters. It does not need a zero at the end unless you want to treat it as a string. By passing it through a %s specifier to printf, you are treating it as a string, so you need to append a zero at the end, otherwise, you're passing something that's not a string through a format specifier that requires a string.

Also p=a => p holds the address of first element of char array a. I don’t understand how it is correct to place an '&' in front of an address (p[0]). &p[0] means address of address of first element of a which doesn't make any sense, at least to me.

It works like this:
p is a pointer to the first element.
&p is the address of the pointer.
p[0] is the first element in the array the pointer points to.
&p[0] is the address of the first element in the array the pointer points to.

Why is it printing the correct output?

Sheer luck. Most likely, the implementation, being 32-bits (4 bytes) couldn't do anything useful with the two bytes after the 6-byte array. So it rounded it up to 8 bytes so that the next thing after it would start at an even 32-bit boundary. So you used two bytes the implementation wasn't using for anything anyway.

Upvotes: 2

exa
exa

Reputation: 940

I hope this helps you a little with understanding the array&pointer relationship:

a[i] == *(a+i) == *(i+a) == i[a]

Upvotes: 0

AndersK
AndersK

Reputation: 36082

when i declared the array as char a[6] why is it allowing me to allocate a value at a[7]?

because C and C++ don't care, there is no bounds check on arrays.

Does it not need a null character to be appended for the last element?

no. e.g. when you declare an array say char a[7]; you tell the compiler you want seven bytes nothing more, nothing less. however if you try to access outside the array it is your problem.

I donot understand how it is correct to mark an '&' infront of an address(marked by p[0]). '&p[0]' means address of address of first element of a[] which is not sensible right?How come it is printing correct output?

if you write p[0] you are referencing the value of the array p e.g. if int p[2] = {1,2}; then p[0] is 1 if you write &p[0] you are getting the address of p[0] which is basically the same as p + 0

Upvotes: 2

David Heffernan
David Heffernan

Reputation: 612993

You have just invoked undefined behaviour. There's little point in reasoning about writing beyond the bounds of an array. Just don't do it.

&p[0] means address of address of first element of a[] which is not sensible

No, that's perfectly sensible. Your description perfectly describes what is going on. &p[0] is the same as p which is the same as a. When you write p[0] you are dereferencing the pointer. When you then write &p[0] you are taking the address of that variable and thus return to what you started from, p.

Upvotes: 9

Ry-
Ry-

Reputation: 224922

It allows you to allocate a value at a[7] because you're lucky. That's undefined behaviour. See here: http://ideone.com/ntjUn

Segmentation fault!

Upvotes: 1

Seth Carnegie
Seth Carnegie

Reputation: 75130

The highest valid index of a is 5, so you're writing outside the array bounds by two, and yes, it still needs a NULL terminator. The fact that it worked was just a coincidence; writing outside array bounds is undefined behaviour. It could work, it could crash your computer, it could go buy pizza with your credit card, or something entirely different. You have no idea what it will do, so just don't do it.

Upvotes: 5

Related Questions