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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:26:40+00:00 2026-05-25T23:26:40+00:00

I have an issue that’s really confusing me… Below I am calling an initialize

  • 0

I have an issue that’s really confusing me… Below I am calling an initialize function:

void Initialize (List *L) {
    char* initialize = "initialize";
    int i;

    for (i=0; i<MAXLISTSIZE; i++) {
       strncpy(L->items[i].name,initialize,MAXNAMESIZE);
       L->items[i].name[MAXNAMESIZE - 1] = '\0';
       L->items[i].grade = 0;
       printf("L->items[i].name = %s\n", L->items[i].name);
       printf("L->items[i].grade = %d\n", L->items[i].grade);
    }    
    L->count = 0;
}

And it seems to work, I print the values in the loop and it’s fine. If I also print inside an identical loop in main to double check it works as well but If I just print the values in main after the initialize function (no print statements in Initialize) I get complete garbage.

It seems the memory I’m storing my values in isn’t staying consistent and I can’t figure out why.

Do I need to malloc memory for the structs? Since I don’t need a variable amount of storage I thought it was not necessary… I am unsure of how to go about that.

My Structs:

typedef Student Item;
#define MAXLISTSIZE 4
typedef struct {
Item items[MAXLISTSIZE];
int count;
} List;

#define MAXNAMESIZE 20
typedef struct {
char name[MAXNAMESIZE];
int grade;   
} Student;

I am simply calling Initialize from main:

int main () {
    List *newList;

    /*call initialize function*/
    newList = callInitialize();

return 0;
}

callInitialize:

List *callInitialize () {
List *L;

List studentList;
L = &studentList;

Initialize(L); 

return L;
}
  • 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-05-25T23:26:41+00:00Added an answer on May 25, 2026 at 11:26 pm

    Now that you posted the function that causes the actual problem, we see what’s wrong: You are returning the address of a local variable that goes out of scope! This is not valid.

    List * foo()
    {
      List x;     // <--- x comes to life
      return &x;  // <--- x dies here...
    }
    
    int main()
    {
      List * p = foo();  // ... but its address is used here!
      p->name ...        // dereferencing an invalid address!!
    }
    

    Your situation calls for dynamic (or “manual”) allocation, which means memory allocation whose lifetime is controlled only by you, and not by the local scope.

    List * initList()
    {
      return malloc(sizeof(List)); // this memory is permanent
    }
    

    Any manual allocation needs to come with a clean-up routine:

    void freeList(List * p)
    {
      free(p);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue with PDO that I'd really like to get an answer
I have an issue that is quite confusing for me and was hoping somebody
I have issue that is reproduced on g++. VC++ doesn't meet any problems. So
I have an issue that seems like very flaky behavour, is this a problem
I have an issue that looks like a race condition with a webview callback
I have an issue that when I leave the android device idle for a
I have an issue that I'm basically baffled by. To start off, I have
Heyy can somebody help me ? i have this issue that still do know
I have a strange issue that has arisen recently: Whenever I enter text, even
I am just new to programming in Unix and have a small issue that

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.