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

  • Home
  • SEARCH
  • 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 340187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:35:34+00:00 2026-05-12T10:35:34+00:00

In C specifically (i suppose this also applies to C++), what is the difference

  • 0

In C specifically (i suppose this also applies to C++), what is the difference between

char str[4] = "abc";
char *cstr = {"abc"};

Problems arise when i try and pass my “abc” into a function that accepts char**

void f(char** s)
{
  fprintf(stderr, "%s", *s);
}

Doing the following yields a compiler error. If cast to char** (to make compiler happy) program seg faults.

f(&str);

However the following works fine

f(&cstr[0]);
  • 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-05-12T10:35:34+00:00Added an answer on May 12, 2026 at 10:35 am

    This is an example of how pointers and arrays are not equivalent in C. In particular: the rule that arrays decay to pointers is not applied recursively

    This means that an array can be used as a pointer, but a pointer-to-array cannot be used as a pointer-to-pointer. This is what you are experiencing here. This is why the compiler complains about mismatched types when you don’t cast &str explicitly to char**. That should be your first clue that something is wrong.

    The reason that this causes a segfault is this: The way that an array automatically decays to a pointer is by turning into the address of its first element. A pointer to an array is likewise a pointer to the address of the array’s first element. So a pointer-to-array and array-as-pointer are the same thing. In other words str, when passed as a pointer, has a value identical to &str. So if you try to make &str into a pointer-to-pointer, it doesn’t work, since is just a (single-level) pointer.

    For example,

    void f(char** pp);
    void g(char* p);
    
    char[] str = "abcd"; // Lets say this is allocated at address 0x1234
    g(str); // Value of p in g is 0x1234 (by automatic conversion of char[4] to char*)
    
    char* p_str = &str; // Value of p_str is 0x1234
    g(p_str); // Value of p in g is again 0x1234 
    
    f(str);  // Illegal, no conversion of char[] to char** (obvious)
    f(p_str); // Illegal, no conversion of char* to char** (obvious)
    f(&str); // Illegal, no conversion of char*[4] to char** (less obvious)
    
    f((char**)p_str); // Ok, now you're overriding the typecheck
    

    But after that last call to f((char**)p_str), the value of pp in f is still going to be 0x1234 because you haven’t modified the value of p_str, you’ve only suppressed the type-checker’s complaint. This means that *pp is going to be ‘a’, not a pointer to the address that contains ‘a’. And that’s why you get a segfault when f tries to execute **pp.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.