in Programming in C
1,295 views
0 votes
0 votes

Consider the following C program

#include<stdio.h>
int main(){
    char *arr={"GATE","CAT","IES","IAS","PSU","IFS"};
    call(arr);
    return 0;
}
void call(char **ptr){
    char **ptr1;
    ptr1=(ptr+=(sizeof(int)))-2;
    printf("%s",*ptr1);
}

Assume size of int pointer 4B.What will be output?

in Programming in C
by
1.3k views

4 Comments

@Satbir

 just checked the problem

but not Garbage returning.

Can u plz share ur solution, so that we can discuss it more??

0
0
Done.
0
0

1 Answer

1 vote
1 vote
Best answer

$\rightarrow$When string constants are created then they are allocated a random memory space and all string constants end with '\0'.

$\rightarrow$So here the string constants are not allocated contiguous memory locations. First string constants are created at random memory locations and then that memory locations are pointed by array elements.

$\rightarrow$When we write like

char *arr[ ] ={"GATE","CAT","IES","IAS","PSU","IFS"};

$\rightarrow$Then each $arr[i]$ will point to their respective string constant's first char's location.


char *arr={"GATE","CAT","IES","IAS","PSU","IFS"};

$\rightarrow$We are assigning array of string constants to a character pointer named $arr$

$\rightarrow$and not to an array of character pointers

$\rightarrow$so this constant char pointer($arr$) will just point to the first char of string constant named  "GATE" among the given string constants and the rest of the string constants will not be allocated any pointer.

G A T E \0

$\rightarrow$$arr$ points to memory location at which G is stored (say $1000$)

call(arr); // passes 1000 to **ptr

void call(char **ptr){ //ptr contains 1000.
    char **ptr1;       // a pointer which will point to address of a char pointer.
    ptr1=(ptr+=(sizeof(int)))-2; // lets decode it step by step.

$\rightarrow$ptr+=(sizeof(int)) $\Rightarrow$ ptr = ptr + 4 = 2000 + 4*(size of ptr) = 2000 + 4*8 =2032

$\rightarrow$here if size of int = 4 so sizeof(int) returns 4.

$\rightarrow$But we dont know size of $ptr$ so it will point at some random memory location.(I am assuming that $ptr$ takes 8 B.)

NOTE :- $ptr$ is not pointer to char. It is pointer to a char pointer.

$\rightarrow$(ptr+=(sizeof(int)))-2 $\Rightarrow$ ptr = ptr - 2 = 2032 - 2*8 = 2016.

ptr1 = (ptr+=(sizeof(int)))-2; // ptr1 points to address 2016.
printf("%s",*ptr1); //prints some garbage value.
selected by

12 Comments

ptr+=(sizeof(int)) ⇒ ptr = ptr + 4 = 2000 + 4*(size of ptr) = 2000 + 4*8 =2032

what have u done here??

0
0
ptr+=(sizeof(int))

=> ptr = ptr + sizeof(int)

now sizeof(int) returns 4 as size of integer is 4 B

=> ptr = ptr +4

adding 4 means moving 4 ptr space ahead and ptr occupies 8 B

=> ptr = ptr + 4*(size of ptr) = ptr + 4*8 = ptr +32 = 2000+32
0
0
edited by

U have taken integer size 4B, not 2 B,

right??

and given int pointer size 4 B

Now how r taking size of pointer 8B??It is given 4B

but u took

ptr = ptr + 4*(size of ptr)

0
0
The size of int pointer is given as 4B and

$ptr$ is a double char pointer so i am assuming its size as $8B$ since it is a pointer to a char pointer and it is not an integer pointer.

Also size of integer is not mentioned in question. so i took 4B.

Just to show that the output will be garbage.

Also this 4B and 8B are not mentioned in question and they will be compiler dependent.
0
0
edited by

 

double char pointer so i am assuming its size as 8B

u mean u taking like this

 $ptr = ptr + (\text{size Of Integer})*(\text{size Of Char Ptr})$    // not size of (int pointer)

i.e. $ptr = ptr +4\times 8$

             $=2000+32=2032$

rt??

0
0
Yes
0
0
and now

$2032-2\times 8=2016$

i.e.IES
0
0
edited by

Read the 2nd statement of my answer.

all string constants are not allocated contigous memory.

$arr$ only knows about "GATE".

When we do like char arr[10][10] = {"GATE","CAT","IES","IAS","PSU","IFS"};

Then we know that all the rows will take 10 cells so we allocate string constants in contiguous manner each occupying a row of 10 cells.

 

but when do like

char *arr[ ] ={"GATE","CAT","IES","IAS","PSU","IFS"};

we don't know how much space will each string constant take so it will not be given contiguous memory locations. instead pointers to each of the string constant will be created which stores the address of 1st char of each od the string constants.

0
0
Also ptr should point to a char pointer right and not to a char ?

where is that char pointer to whom ptr will point ?
0
0

See here https://gateoverflow.in/249237/madeeasy-test-series-programming-%26-ds-programming-in-c

we can take integer pointer instead of char pointer

And u mean garbage because while printing, we r printing with single pointer , instead of double pointer

rt??

0
0
edited by

The link you mentioned has this

char *arr[ ] ={"GATE", "CAT", "IES", "IAS", "PSU", "IFS" };

in this case an array of char pointers will be created and each $arr[i]$ will point to their respective string constant's first char's location as i have shown in the above daigram.

here arr+2 will point to "IES"

and our question is

char *arr={"GATE","CAT","IES","IAS","PSU","IFS"};

So in our case array of char pointers will not be created we will just have $arr$ pointing to "GATE" i.e. a normal char pointer pointing to "GATE". all other strings will be lost.we will not be able to access them sice they are not contigously stored as i shown above.

here arr+2 will point to "T" to "GATE"

This is the 1st error in the question.


2nd error is in ptr1 = (ptr+=(sizeof(int)))-2;

The link you mentioned has mentioned that in that question, they mentioned that " size of int = size of pointer = 4 Bytes "  and in this question you have not mentioned it. (read the 12th comment in that question)

we can't take integer pointer instead of char pointer.

I am saying garbage because it will create segmentation fault because we dont know what 2016 contains.

0
0

And u mean garbage because while printing, we r printing with single pointer , instead of double pointer rt??

This is also true.We are not deferencing correctly.

0
0

Related questions