0

Friend I am new in c, I am trying to print string using in array of pointer I am following the code from online tutorial http://www.tutorialspoint.com/.my code link is Here's a link! all things are ok no error, when I run the the code message is shown: [Warning] deprecated conversion from string constant to 'char*' [-Wwrite-strings] there is no output, is the code in online is wrong, or what is fault can any one give me some idea my code is below:

#include <stdio.h>

const int MAX = 4;

int main ()
{
   char *names[] = {
                   "Zara Ali",
                   "Hina Ali",
                   "Nuha Ali",
                   "Sara Ali",
   };
   int i = 0;

   for ( i = 0; i < MAX; i++)
   {
       printf("Value of names[%d] = %s\n", i, names[i] );
   }

   return 0;
}

If any fault of me, I copying code, I am newer in c so I am learning from online tutorial, so please don't give any negative comment, if it also duplicate, kindly closed the question.

3
  • If you want to learn C, I suggest you use a C compiler; the use of a C++ compiler to learn C++ is adequate :) Commented Apr 8, 2014 at 14:30
  • Use names[i], not *names[i]. Commented Apr 8, 2014 at 14:31
  • Tip: Avoid magic constants. If you use them, make sure you get an error if they are no longer appropriate. (sizeof names/sizeof *names) is the number of elements in array names. Alternatively / additionally, put the constant into the array definition too: char *names[MAX] = ... Commented Apr 8, 2014 at 14:36

2 Answers 2

1

Remove * from *names[i] in printf;

printf("Value of names[%d] = %s\n", i, names[i] );
Sign up to request clarification or add additional context in comments.

2 Comments

Dangit, that's what I thought but wasn't 100% sure. Should have put it down.
i removed it but nothing is changed
0

[Warning] deprecated conversion from string constant to 'char*' means, you have string literal, which are read-only, but you have a pointer to it which allows modification. So compiler warns you about that. To get rid of the warning, this should declare names as array of pointers to const char, like this:

const char *names[] = { /*...clipclip... */ };

It is "just" a warning, because old code often had such pointers, and making that an error now would prevent that from compiling. Also, this issue is no problem as long as you don't try to modify the string literals. If you do, you should get a segmentation fault or equivalent at runtime on any modern OS. Also, sometimes you might want to initialize a char* variable to point to string literal, then if necessary, change it to point to a new modifiable buffer, but any such use case should be documented with comments, and warning disabled by using an explicit cast char *s = (char*)"defaultvalue";.

1 Comment

thanx, hyde, for your answer, i got the correct answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.