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 767315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:00:36+00:00 2026-05-14T18:00:36+00:00

Why does static char *opcode_str[] = { DATA , DATA_REQUEST_ACK , ACK_TIMER_EXPIRED , ACK_UNEXPECTED_SEQ

  • 0

Why does

static char *opcode_str[] = { "DATA"
                            , "DATA_REQUEST_ACK"
                            , "ACK_TIMER_EXPIRED"
                            , "ACK_UNEXPECTED_SEQ"
                            , "ACK_AS_REQUESTED"
                            } ;

work, but

static char **opcode_str = { "DATA"
                           , "DATA_REQUEST_ACK"
                           , "ACK_TIMER_EXPIRED"
                           , "ACK_UNEXPECTED_SEQ"
                           , "ACK_AS_REQUESTED"
                           } ;

fails with SEGV when opcode_str[0] is printf’d?

I think it’s because the second listing has not allocated memory for the five element array of pointers, but I need a more comprehensive explanation.

All the best,

Chris.

  • 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-14T18:00:37+00:00Added an answer on May 14, 2026 at 6:00 pm

    That’s correct. You’re essentially trying to assign an array to a pointer. GCC 4.4.1 warns about this by default:

    opcode_str.c:4: warning: initialization from incompatible pointer type
    opcode_str.c:5: warning: excess elements in scalar initializer
    

    It repeats the excess elements warning 4 times, since you’re essentially putting 5 pointers where only one will fit. You can use gcc -Werror to force all warnings to be errors.

    You could do:

    static char **opcode_str = malloc(sizeof(char *) * 5);
    opcode_str[0] = "DATA";
    opcode_str[1] = "DATA_REQUEST_ACK";
    opcode_str[2] = "ACK_TIMER_EXPIRED";
    opcode_str[3] = "ACK_UNEXPECTED_SEQ";
    opcode_str[4] = "ACK_AS_REQUESTED";
    

    But you’ve already found the best way to do it. As far as when the error occurs, once you invoke undefined behavior you really can’t count on a particular time for problems to manifest.

    But I think opcode_str holds a pointer to DATA. So (assuming 32-bit) it will try to interpret the first four bytes at opcode_str (‘D’,’A’,’T’,’A’) as as the four bytes of a char*.

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

Sidebar

Ask A Question

Stats

  • Questions 512k
  • Answers 512k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I've had good luck with Sensible TableView. The support is… May 16, 2026 at 5:37 pm
  • Editorial Team
    Editorial Team added an answer require 'net/http' url = URI.parse('http://www.3rdparty.com/api.php?f=add_entry') args = {'arg1' => 'data'… May 16, 2026 at 5:37 pm
  • Editorial Team
    Editorial Team added an answer This is essentially what the new() constraint boils down to:… May 16, 2026 at 5:37 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

sleep() is a static method of class Thread. How does it work when called
char * function decode time() { tm *ptm; //time structure static char timeString[STRLEN]; //hold
Does this leak?: static PyObject* foo(PyObject* self, PyObject* args){ PyObject* list = PyList_New(0); for(int
Why does this fail to compile? (g++-4.5) template < typename U > static void
Does this seem correct? I'm trying to port this checksum method from C to
Does the Cyrus SASL api not support the EXTERNAL mechanism? I'm trying to use
My question is why does the address of an array differ from the address
Im working in a memory restricted environment and need to create strings dynamically, but
Trying to create an app that does some socket communication (Writing only). I can
I am trying to define a public static variable like this : public :

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.