LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   string in c with pointers ! (https://www.linuxquestions.org/questions/programming-9/string-in-c-with-pointers-4175416249/)

tushar_pandey 07-11-2012 09:33 PM

string in c with pointers !
 
when i am creating a C program , in which i am using Strings with the help of pointers !

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
char *ptr = "hello"; // line :: 1

printf("%s\n",*ptr); // line :: 2

return 0 ;
}

so ,

at line :: 1 , i am facing this problem ::
warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

at line :: 2 , i am facing this problem ::
warning: format ‘%s’ expects argument of type ‘char*’, but argument 2 has type ‘int’ [-Wformat]

firstfire 07-11-2012 11:55 PM

Hi.

And the question is...?

The second warning is actually an error, because you provide character `h' (*ptr == 'h' in your case) instead of a pointer to a memory location (that is instead of a string).

NevemTeve 07-12-2012 12:49 AM

Code:

int main (void)
{
    const char *ptr = "hello"; // line :: 1

    printf ("%s\n", ptr); // line :: 2

    return 0 ;
}



All times are GMT -5. The time now is 01:22 PM.