in Programming in C edited by
2,510 views
0 votes
0 votes
#include <stdio.h>
#include<stdlib.h>
int main() {
    //code
    char *ptr,s[]="debasree";
    ptr=s[0];
    printf("%s\n",*ptr);
    return 0;
}

 

I get this warning in geeks for geeks compiler

Warnings:

prog.c: In function 'main':
prog.c:7:6: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
   ptr=s[0];
 

prog.c:13:8: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat=]
 printf("%s\n",*ptr);
     ^

and also get 

Runtime Errors:

Segmentation Fault (SIGSEGV)

can anyone pls explain what is the warning saying? i googled a lot but could not grasp it… pls pls pls pls explain...

in Programming in C edited by
2.5k views

2 Comments

ptr=s[0] ,  here i think typecasting is required makes pointer from integer
0
0
type changing from $int\left [  \right ]$ to  $int *$

but should give compilation error
0
0

1 Answer

1 vote
1 vote
Best answer

s[0] is a single character, you just have to do ptr=s or if you wanna go crazy maybe &(s[0]). Also, the main problem is 

printf("%s\n",*ptr);

It should be 

printf("%s\n",ptr);

Google out how to print strings using printf and go through more examples of pointers to clear your basics.

Also, see the line number in error its 7, see what's line 7. That's how you start debugging.

selected by

4 Comments

@Anuj Mishra

We donot need to write 1. and 2. line at the same time

right?

1.ptr=&s[0];

2. *ptr=s[0];

printf("%c\n",*ptr);

Because, if we change the pointer or value at that pointer, that meaning same here.

right?

0
0
Only 1 will work , 2 doesn't give the starting address of string to ptr, so you can't print string using ptr, Also I think it'll give error because ptr is not initialised with any address so where will the *ptr refer to.
1
1
yea, rt.
0
0