0

please help to fixed this program. I'm try to print array of pointer using pointer instead of array but i got this error:

pointer_multi_char4.c: In function ‘main’:
pointer_multi_char4.c:7:11: error: expected expression before ‘{’ token

this is the code:

#include <stdio.h>

int main (void){
char **message;
message= { "Four", "score", "and", "seven",
               "years", "ago,", "our", "forefathers" };
printf("%s\n",message);
return 0;
}

how can i fixed this code? please somebody explain me what's wrong with that code

2
  • Also, are you sure you don't just want to be using a char* to the string "Four score and seven years ago our forefathers"? Commented Feb 19, 2014 at 21:51
  • char** != char*[]. Commented Feb 19, 2014 at 21:52

2 Answers 2

5
#include <stdio.h>

int main (){
    char *message[] =  { "Four", "score", "and", "seven",
                         "years", "ago,", "our", "forefathers", 0 };

    int loop;
    for (loop = 0; message[loop]; ++loop) printf("%s\n",message[loop]);
    return 0;
}

The braces in this case (making an assumption) is that you want to initialise an array (hence the use of char *message[] instead of char **.

As it is an array need to loop over it. I used a null pointer to mark the end of the array

EDIT

Then @Lundu just need

#include <stdio.h>

int main()
{
    const char * mesage="Four scor and seven years .... forefathers";
    printf("%s\n", message);
    return 0;
}
Sign up to request clarification or add additional context in comments.

3 Comments

What is worth mentioning, that in @EdHeal example the initialization is in the same line as declaration. If you make it as two separate instructions, it's no longer an initialization - it's an assignment.
I'm tryng to print this message using pointer instead of array,
@Smac89 - More speed less haste
1
#include <stdio.h>

int main (void){
    char **message;
    message= (char* []){ "Four", "score", "and", "seven",
               "years", "ago,", "our", "forefathers" };
    int numOfMessage = 8;
    while(numOfMessage--){
        printf("%s\n", *message++);
    }
    return 0;
}

Comments

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.