in Programming in C
1,271 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

4 Comments

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