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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:48:10+00:00 2026-06-03T11:48:10+00:00

I asked the question: Pass array by reference using C . I realized my

  • 0

I asked the question: Pass array by reference using C. I realized my problem was the usage of the pointer star in C. And eventually it turned out that, this method works fine for my program:

#include <stdio.h>

void FillArray(int** myArray)
{   
     *myArray = (int*) malloc(sizeof(int) * 2);

     (*myArray)[0] = 1;
     (*myArray)[1] = 2;
}

int main()
{
     int* myArray = NULL;
     FillArray(& myArray);  
     printf("%d", myArray[0]);
     return 0;
}

Everyting was fine up to that point. Then, I modified the FillArray() function like the following for a better code readability:

#include <stdio.h>

void FillArray(int** myArray)
{
     int* temp = (*myArray);

     temp = (int*) malloc(sizeof(int) * 2);
     temp[0] = 1;
     temp[1] = 2;
}

int main()
{
     int* myArray = NULL;
     FillArray(& myArray);  
     printf("%d", myArray[0]);
     return 0;
}

Now, I get the following run-time error at the printf line:

Unhandled exception at 0x773115de in Trial.exe: 0xC0000005: Access violation reading location 0x00000000.

Even though I’m not an C expert, it seemed legitimate to do this modifying. However, apparently it doesn’t work. Isn’t the syntax a little bit confusing? Do I miss something here?

Thanks for your helpful answers,

Sait.

  • 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-03T11:48:15+00:00Added an answer on June 3, 2026 at 11:48 am

    temp gets a copy of the address of myArray, but then you assign some malloced memory to temp, so the original assignment was pointless and had no lasting effect. You then modify the malloced memory, but that doesn’t change myArray at all. To change myArray in main, you have to assign

    *myArray = temp;
    

    at the end of FillArray.

    void FillArray(int** myArray)
    {
         int* temp;
         temp = (int*) malloc(sizeof(int) * 2);
         temp[0] = 1;
         temp[1] = 2;
         *myArray = temp;
    }
    

    does what you intend.

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

Sidebar

Related Questions

This question builds off of a previously asked question: Pass by reference multidimensional array
I was asked this question in an interview. If there is a pointer that
I previously asked this question here: Stop user from using enter to pass a
I asked a question here about an Linq error that results from mixing Linq-To-SQL
I was asked this question recently in a test, which I didn't pass. For
The reason I asked this question is that I wanted to create a helper
I recently asked a question about using the Page.User or HttpContext.Current.User in the View.
i asked a previous question on this website and it got answered using pseudo
This is a follow up of another question I asked last night. My problem
I know that this question might have been asked like 100 times, but, believe

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.