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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:27:22+00:00 2026-06-10T20:27:22+00:00

I have the following C code which works: #include <stdio.h> #include <stdlib.h> #include <string.h>

  • 0

I have the following C code which works:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

int pw = sizeof(char*);     // width of pointer (to char)

int num;
int first = 1;
int size = 0;
int incr = 10;

char *(*arr)[];     // pointer to array of pointers to char */

test(char* s, int i)
{

  int j;
  char *(*newarr)[];        // pointer to array of pointers to char

  if (first) {          // first time
    arr = malloc(pw*incr);  // malloc array
    first = 0;          // skip from now on
    size = incr;        // save the size
  }


  if (i >= size) {          // out of space
    newarr = malloc(pw*(size+incr));    // get incr bigger space
    for (j=0; j<size; j++)      // copy the elements from the old
      (*newarr)[j] = (*arr)[j];     // array to new array
    free(arr);                  // free the old array space
    arr = newarr;           // point old array to new array
    size = size+incr;

  };

  int len = strlen(s);      // length of s
  (*arr)[i] = malloc(len+1);    // assign pointer to pointer array element
  strcpy((*arr)[i], s);     // copy s to array
                    // both arguments must be pointers

  printf("%d\t%s\n", i, (*arr)[i]);
};

main() 
{

  char* s = "this is a string";

  for (num=0; num<30; num++)    // add 30 pointers to s to *arr
    test(s, num);

  for (num=0; num<30; num++)
    printf("%d\t%s\n", num, (*arr)[num]); // print out what they point to
};

It prints out ‘i\tthis is a string’ for ‘i’ from 0 to 29 twice. What I want to do is pass ‘arr’ from the top of the file as an argument of ‘test’. The reason I want to do that is because I want to pass several different arrays all of which are declared the same way. If I make the minimal changes to do that I get:

0   this is a string
Segmentation fault (core dumped)

Here is the output of the diff command which shows the minimal changes:

    13c13
< char *(*arr)[];       // pointer to array of pointers to char */
---
> char *(*jarr)[];      // pointer to array of pointers to char */
15c15
< test(char* s, int i)
---
> test(char* s, int i, char *(*arr)[])
52c52
<     test(s, num);
---
>     test(s, num, jarr);
54,55d53
<   for (num=0; num<30; num++)
<     printf("%d\t%s\n", num, (*arr)[num]); // print out what they point to

In other words everything is the same except for renaming ‘arr’ as ‘jarr’ and passing it to ‘test’.

Thanks in advance,
Mike

  • 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-10T20:27:23+00:00Added an answer on June 10, 2026 at 8:27 pm

    The trouble occurs when you call:

    test(s, num, jarr);
    

    You are passing jarr by value. Inside the function, you are reallocating (the hard way — why not use realloc() which does the copying for you?) the array, but that change does not affect the value of jarr ‘in main()‘ because it was passed by value. The second time through the loop, you are still passing a null pointer to the function, but you are then dereferencing that null pointer, which is bad news.

    How to fix?

    Fair question…I’m not sure if the old “well, if I want to get to there, I wouldn’t start from here” gag passes muster.

    The ‘simplest’ change is to revise the call:

    jarr = test(s, num, jarr);
    

    and then ‘just’ revise the function so that it returns a pointer to an array of character pointers. That is a very esoteric function. My brain’s not awake (insufficient caffeine), so I used an intermediate typedef to get around the problem of how to write the function declaration and definition:

    typedef char *(ArrayString[]);
    
    ArrayString *test3(char *s, int i, char *(*arr)[]);
    
    ArrayString *test3(char *s, int i, char *(*arr)[]) { (*arr)[i] = s; return arr; }
    

    It compiles without warnings; that isn’t a guarantee that it’s correct.

    The primary alternative is to pass a pointer to a pointer to an array of char pointers to the function, which is even more esoteric.


    However, both of these are ‘starting from here’ solutions. You’d do better, on the whole, to devise a different way of handling things. Pointers to arrays are certainly a part of C, but they are at the outer edges of C and you should generally assume that if your design calls for their use, then your design is probably not the best. You should use a simpler char ** (or, perish the thought, char ***; triple indirection is best avoided too, but that isn’t always possible).

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

Sidebar

Related Questions

I have the following code: #include <stdio.h> #include <time.h> #include <stdlib.h> #include <unistd.h> int
I have the following code: #include<stdio.h> int main() { int(* a)[10]; //declare a as
I have the following code which works as expected: #include <iostream> using namespace std;
I have following code which works for radio buttons but need to be changed
I have the following code which works perfectly fine, however I would like to
I have the following code which works perfectly, but I want to allow access
I have the following code which works when i put it in any blank
At the moment I have the following code which works fine. label = new
I have trouble to get gluLookAt working. I have the following code which works
I have the following code below which works, but I want to insert values

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.