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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:31:16+00:00 2026-06-06T09:31:16+00:00

I am struggling really hard to understand this behavior so maybe someone can shed

  • 0

I am struggling really hard to understand this behavior so maybe someone can shed some light on the situation.
I simply can’t figure out why I can’t return a pointer to a struct from a method and expect to be able to still re-use it afterwards.

As you can see the generateSmallMatrix() method creates an int[] array and sets it inside the ysmf struct that I then return to main. Main then takes the ysmf* and calls printArray (again). And on the third try the array cannot be retrieved any more..
It’s driving me crazy..

I have checked with my Eclipse debugger that on all calls the location of matrix->A is identical (0x7fffffffe180 – so for all I know about C pointers any form of accessing that int should return the correct value – be it *(ax++) or ax[i]) .. but neither do..

Very frustrating to say the least, so here is the code:

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

typedef struct sparseMatrix
{
    int* A;
} ysmf;

void printArray(int* ax, int length) {
    int i = 0;
    for (i = 0; i < length; i++) {
        printf("%i,", ax[i]);
    }
    printf("\n");
}

ysmf* generateSmallMatrix()
{
    ysmf *matrix = malloc(sizeof(ysmf));

    int a[] = {1,2,3,9,1,4};
    printArray(a, 6);                   // returns 1,2,3,9,1,4,
    matrix->A = a;
    printArray(matrix->A, 6);           //returns 1,2,3,9,1,4,
    //printArray(matrix->A, 6);

    return matrix;
}


int main(void) {
    ysmf* matrix = generateSmallMatrix();
    printArray(matrix->A, 6);           //returns 1,6,-7856,32767,1,4,
    return EXIT_SUCCESS;
}

You can see the problem occuring where I have inserted the comments.

I know this is probably something totally basic I shouldn’t have missed..

  • 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-06T09:31:18+00:00Added an answer on June 6, 2026 at 9:31 am

    Because a is a local array, whose lifetime ends when the generateSmallMatrix() function ends. Accessing it after that results in undefined behaviour.

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

Sidebar

Related Questions

Really struggling to figure this out. Just trying to take in data from php
I'm sure this can't be too hard; but I'm struggling. The error is Exception
Really struggling to understand this problem, any ideas are welcome I have a carousel
I'm really struggling with a MySQL query that I'm really hoping someone can help
I'm really struggling to wrap my head around some of this stuff. Let me
Im really struggling to get my head round this. Im using c#. I want
I'm really struggling with this. I have a viewdetails.php page, addnew.php page and a
I'm really struggling to understand cocos2d 2.0 and how its layers and coordinates work.
I am really struggling to understand why, when I change my code to use
I'm really struggling with this now, been on it for ages and asked two

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.