in Programming in C edited by
525 views
0 votes
0 votes

Find the out put of the following C program.    

main()
{
    char *ptr = "techtud";
    char p= (*ptr)++;
    printf("%s\n",ptr);
}

the output of the program came as$?$

in Programming in C edited by
525 views

3 Comments

Did you compile it ?
0
0
yes i compiled and ran it. It gave the correct result without any errors or warnings.
0
0

Not able to run

0
0

1 Answer

1 vote
1 vote
first of all char *p = "techtud"  is wrong
char *p means , p can hold only address not a string
to assign it a string u need to assign memory dynamically at runtime or make it as char p[] instead of char* p

if we ignore error and talk about logic then (*ptr)++  this line does this
(*ptr)  means value at address that ptr hold and ptr holds base address of array having techtud
so *ptr == t
now (t)++  which is (ascii value of t)++  which is equals to u   since it is post increment changes will take place in actual variable to which ptr is pointing
and p will will assigned t coz of post increment
by

1 comment

edited by

when i run it in geeks for geeks compiler i get segmentation fault... i modify it this way...

#include <stdio.h>
#include<stdlib.h>
int main() {
    //code
    char *ptr;

//dynamically allocating an array 

ptr=(char*)malloc(10*sizeof(char));

*ptr="techtud";

char p=(*ptr)++;

printf("%s\n",ptr);
    return 0;
}

then i get this error...

warning: assignment makes integer from pointer without a cast

pls help me... pls explain why this is happening

0
0

Related questions