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

Related Questions

#include<stdio.h> int main() { static char *s[]={black,white,pink,violet}; char **ptr[]={s+3,s+2,s+1,s},***p; char a[]={DEAD}; p=ptr; ++p; printf(%c\n,a[0]);
error C2129: static function 'void func(int,char *)' declared but not defined I read that:
What does public static void mean in Java? I'm in the process of learning.
If I have a class called myclass does having static methods within the class
I would like to have a mapping function that does this: public static void
Does the latest version of MacRuby support static compilation? Here's some terminal output from
Does the stack of a thread/process make seperate copy of static function/method or is
Does anyone know why a library initialized within dlopen() would initialize a static variable
I have a static util class that does some string manipulation on a bit
Does a method like this exist anywhere in the framework? public static void Swap<T>(ref

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.