Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8662385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:43:33+00:00 2026-06-12T16:43:33+00:00

Forgive me if this is a simple error, but I am just learning C

  • 0

Forgive me if this is a simple error, but I am just learning C at the moment and I just made a trivial program to get a grasp of pointers.

I have a simple piece of code that yields an output I expect

int x = 4;
int *p;
p = &x;
printf("%d\n\n",*p);

//output is 4 as expected

But when I try to do the same with a char array even though I follow the same logic…

char x[] = "Hello, Stack Overflow!";
char *p[];
p = &x;
printf("%s\n\n",*p);

//this gives me an error when compiling as follows
//
// run.c:15: error: incompatible types when assigning to type ‘char *[1]’ from type ‘char (*)[23]’
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:43:35+00:00Added an answer on June 12, 2026 at 4:43 pm

    Multiple errors. First, char *p[] doesn’t declare a pointer-to-an-array (as you think it does) – it rather declares an array of char pointers.

    Second, since x is an array, &x will evaluate to the same numerical value as x (since arrays cannot be passed by value, only by pointer, they decay into pointers when passed to a function). What you need to do is rather

    const char *x = "Hello SO!";
    const char **p = &x;
    printf("%s\n", *p);
    

    This is the easy solution (making the string literal, that is, an array of chars, decay into a pointer). There’s another solution that requires a bit more thinking. From the compiler error message, you can see that the type of &x is char (*)[]. So what you have to declare here is a pointer-to-array:

    char x[] = "Hello SO!"; // x is a char array
    char (*p)[] = &x; // p is a pointer to a char array
    printf("%s\n", *p); // printf accesses *p, so it prints the underlying char array - correct!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Forgive me if this is a simple answer. But how do you get a
This is probably something very simple, so forgive my blonde moment :) I have
Just getting into the NoSQL stuff so forgive me if this is a simple
Please forgive my programming knowledge. I know this is a simple thing, but I
This is probably a very simple question so please forgive my ignorance, but can
Forgive me for this unbeautiful code, but I'm trying to get it to work
I'm new to ruby so forgive me if this is simple or I get
This is simple question but I am a newbie so please forgive my simple
Forgive me if this is a simple problem but I can't seem to find
I'm still learning Objective-C so forgive me if this is a simple amateur mistake,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.