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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:45:01+00:00 2026-06-01T14:45:01+00:00

I have a working C program where the length of an array of strings

  • 0

I have a working C program where the length of an array of strings is known at compile time. It is:

char array_person_name[3][101];
char person_name[101] = "";
...
strncpy(person_name, "John Smith", strlen("John Smith")+1 );
for(i=0;i<3;i++)
{
    sprintf(array_person_name[i], person_name );
}

When I check the first element using gdb it reads (e.g. “John Smith” + null character):

(gdb) p array_person_name[0]  
$1 = "John Smith", '\000' <repeats 26 times>, "\003\000\t\000p\005\240 ?", '\000' <repeats 11 times>, "@\337\377\377\377\177\000\000\260\337\377\377\377\177\000\000\001\000\000\000\000\000\000\000\300\332\377\377\377\177\000\000\000\235\230Z5\000\000\000\260\337\377\377\377"

All the other elements in the array look similar. Now I need to modify this program so it works when the array size is not known at run time, where the result needs to be exactly the same as what is printed from gdb above (except, the characters after the null may be different). Here’s my code:

char **array_person_name;
char person_name[101] = "";
...
strncpy(person_name, "John Smith", strlen("John Smith")+1 );
array_person_name = malloc(3 * sizeof(char*)); /* allocate memory for row pointers */
for(i=0;i<3;i++)
{
  array_person_name[i] = malloc(101 * sizeof(char)); /* allocate memory for columns */
  sprintf(array_person_name[i], person_name );
}

When I then check the first element using gdb, it reads:

(gdb) p array_person_name[0]
$1 = 0x6cff30 "John Smith"

I’m not exactly sure what the difference between these two programs’ gdb outputs are, but whatever difference there is, is enough for a shared library, which accepts array_person_email as an input, to perform correctly for the first code block and incorrectly for the second code block. Is there a way to modify my second code block so that array_person_email looks the same as what results from the first code block?

  • 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-01T14:45:03+00:00Added an answer on June 1, 2026 at 2:45 pm

    The reason the output in GDB looks different is because in the first case, the array_person_name[0] is an array whose size is known at compile-time, so GDB endeavours to display everything it knows about that array (i.e. all 101 elements). If you instead did p (char *)array_person_name[0], you would only get the “relevant” portion of the array.

    If your library function exhibits different behaviour, then either it’s broken, or you’re not passing it what it expects (i.e. it’s expecting something other than a char **).

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

Sidebar

Related Questions

I have a working program in C++ that generates data for a Mandelbrot Set.
I have been working on a childish little program: there are a bunch of
I have a program in Perl I'm working on where I would need multiple
I have a program that I'm working on that register global hotkeys I would
I have this program that I am working on for class, I think the
In a program I'm working on I have String cwd; String file_separator; public ConfigLoader()
I have a requirement for a program I'm working on to store job numbers
I'm working on a vbscript program and I have got and Expected Statement Error.
I have a simple Delphi program that I'm working on, in which I am
Well, I have this program, the download option is working fine. I can access

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.