Omar Khaled
Omar Khaled

Reputation: 25

name of arrays and pointers in c

I have wondered why I can use the name of an array of characters so it brings the whole string between. I can't do the same with types like int and char*

int numbers[]={26,8,1995};
char name[]="Omar";
char* full_name[]={"Omar","Khaled"};
printf("%d",numbers); //random result i know its address of first element
printf("%s",full_name); //random resut i know it is pointer not string
printf("%c",name); //O .. ok 
printf("%p",name); //adress .. ok 
printf("%s",name) //Omar why ! name supposed to be pointer to first char O

Upvotes: 1

Views: 107

Answers (2)

Tibor Takács
Tibor Takács

Reputation: 4808

Lets interpret your codes:

int numbers[]={26,8,1995};

An interger array with two elements. The variable "number" is the address of the array, so it points to the first elements.

char name[]="Omar";

A character array. The variable "name" is the address of the character array, so it points to the first character of the string. Important to note that this character array contains 5 characters, namely all strings is terminated by a \0 character.

char* full_name[]={"Omar","Khaled"};

Here comes the interesing part. The variable "full_name" is an array of character pointers, so it points to an array with length 2 the content of which is 2 pointers.

So, lets see your printing codes:

printf("%d",numbers); //random result i know its address of first element

numbers is a pointer. Because of %d it is interpreted as an interger therefore the printf function prints the address value as an integer.

printf("%s",full_name); //random resut i know it is pointer not string

full_name is also a pointer. Therefore it works same as the previous example: because of %d the pointer is interpreted as an interger therefore the printf function prints the address value as an integer.

printf("%c",name); //O .. ok

It is only a "luck". name is address and its first byte had the same value like 'O' character. Try once more and you will see the difference.

printf("%p",name); //adress .. ok

In this case the address of name array had printed. Check first byte and compare with the previously character in an ASCII table. You will find the same values there.

printf("%s",name) //Omar why ! name supposed to be pointer to first char O

In this case the printf method interprets the "name" variable as a character array. The printf method starts at the first byte and write its character value on the screen (for example, if its value is 0x35 it will write '5' character). Then it jumps to next byte and does the same. The printf method continues this "work" until the first terminator character (0x00 - '\0').

The result printed by printf method on the screen depends on interpretation of the variable.

Upvotes: 1

Jonathan Leffler
Jonathan Leffler

Reputation: 754090

The basic reason why you can use %s to print a null-terminated array of characters and can't do anything similar with an array of integers or an array of char pointers is that printf() has been trained (coded, programmed) to treat an array of characters specially, but has not been trained to handle the array of integers or array of character pointers. All else apart, you'd have to tell it somehow how long the arrays are. For the pointers, that might be a null pointer. For the integers, it is probably not sensible to use a sentinel.

Upvotes: 1

Related Questions