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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:23:06+00:00 2026-06-16T00:23:06+00:00

In this toy code example: int MAX = 5; void fillArray(int** someArray, int* blah)

  • 0

In this toy code example:

int MAX = 5;

void fillArray(int** someArray, int* blah) {
  int i;
  for (i=0; i<MAX; i++)
    (*someArray)[i] = blah[i];  // segfault happens here
}

int main() {
  int someArray[MAX];
  int blah[] = {1, 2, 3, 4, 5};

  fillArray(&someArray, blah);

  return 0;
}

… I want to fill the array someArray, and have the changes persist outside the function.

This is part of a very large homework assignment, and this question addresses the issue without allowing me to copy the solution. I am given a function signature that accepts an int** as a parameter, and I’m supposed to code the logic to fill that array. I was under the impression that dereferencing &someArray within the fillArray() function would give me the required array (a pointer to the first element), and that using bracketed array element access on that array would give me the necessary position that needs to be assigned. However, I cannot figure out why I’m getting a segfault.

Many thanks!

  • 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-16T00:23:06+00:00Added an answer on June 16, 2026 at 12:23 am

    I want to fill the array someArray, and have the changes persist outside the function.

    Just pass the array to the function as it decays to a pointer to the first element:

    void fillArray(int* someArray, int* blah) {
        int i;
        for (i=0; i<MAX; i++)
            someArray[i] = blah[i]; 
    }
    

    and invoked:

    fillArray(someArray, blah);
    

    The changes to the elements will be visible outside of the function.

    If the actual code was to allocate an array within fillArray() then an int** would be required:

    void fillArray(int** someArray, int* blah) {
        int i;
        *someArray = malloc(sizeof(int) * MAX);
        if (*someArray)
        {
            for (i=0; i<MAX; i++)  /* or memcpy() instead of loop */
                (*someArray)[i] = blah[i];
        }
    }
    

    and invoked:

    int* someArray = NULL;
    fillArray(&someArray, blah);
    free(someArray);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got this toy code, works fine, using MySQL var r = new SimpleRepository(DB,
This is toy-example.hs: {-# LANGUAGE ImpredicativeTypes #-} import Control.Arrow data From = From (forall
How can I make boost.python code python exceptions aware? For example, int test_for(){ for(;;){
I'm using Git to track some matlab code. A toy example best illustrates the
As a toy example, suppose that we have a function called 'my_func' (the code
I have a toy program: $ cat a.hs main = putStrLn Toy example $
I have string looking like this: 'Toy Story..(II) (1995)' I want to split the
This is a toy project I'm working on at the moment. My app contains
this is my first question in here, and I would like to ask if
This is my code: <CheckBox android:id=@+id/sprint_checkbox android:layout_width=fill_parent android:layout_height=wrap_content android:text=@string/sprint_game /> <CheckBox android:id=@+id/marathon_checkbox android:layout_width=fill_parent android:layout_height=wrap_content

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.