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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:41:24+00:00 2026-06-13T06:41:24+00:00

I am absolutely new to C and I tried to initialize a array in

  • 0

I am absolutely new to C and I tried to initialize a array in a function.

But it doesn’t work, because if I want to print the values in the main method I always get a Segmentation fault.

 static void array(int *i)
{
    int j = 0;
    i = (int *) malloc(5 * sizeof (int));
    for (j = 0; j < 5; j++) {
        i[j] = j;
    }

    for (j = 0; j < 5; j++) {
        printf("Hello: %d\n", i[j]);
    }
}

/* Main entry point */
int main(int argc, char *argv[])
{
    int j;
    int *i = NULL;

    array(i);
    for (j = 0; j < 5; j++) {
        printf("Hello: %d\n", i[j]);
    }
    return 0;
}

Would be nice if someone could fix the code and could explain how it works.

  • 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-13T06:41:26+00:00Added an answer on June 13, 2026 at 6:41 am
    static void array(int **i)
    {
        int j = 0;
        *i = malloc(5 * sizeof (int));
        for (j = 0; j < 5; j++) {
            (*i)[j] = j;
        }
    
        for (j = 0; j < 5; j++) {
            printf("Hello: %d\n", (*i)[j]);
        }
    }
    
    /* Main entry point */
    int main(int argc, char *argv[])
    {
        int j;
        int *i = NULL;
    
        array(&i);
        for (j = 0; j < 5; j++) {
            printf("Hello: %d\n", i[j]);
        }
        return 0;
    }
    

    You are passing a pointer by value into array, so what you need to do is pass a pointer to your pointer instead, then set/use that.


    As for why you shouldn’t cast the result of a malloc, see: Do I cast the result of malloc? and
    Specifically, what's dangerous about casting the result of malloc?

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

Sidebar

Related Questions

I'm absolutely new to GWT, java and eclipse, but I'm an experienced MS programmer.
This might be a very simple problem indeed, but I am absolutely new to
I want to try new solr collapsing/grouping included in solr 3.3, i have tried
I'm absolutely new to JavaScript and would like to modify a textarea of a
First of all, I am absolutely new to WP. I am hoping to build
I am absolutely and totally new to rails, so the answer is probably very
I'm new to Rx and am absolutely loving it. I've found existing parts of
I'm writing a work management application in C# for my team at work (because
As soon as I add the MediaPlayer.framework into a new program, XCode absolutely shits
I am absolutely new to Java and having trouble referencing a class that I

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.