简体   繁体   中英

C code passing struct into function Stack overflow

Okay so I'm having an issue with a current assignment (trust me this is a minuscule part of it) as we are required to write in C code and not C++, and we are not allowed to change certain parts of code. So I have a struct defined:

    typedef struct someStruct {
    int what;
    int something[MAX];
    int another[MAX];
} someType;

in main() I initialize all the values in a defined struct:

someType whatever, *whatptr;

EDIT:of course set the pointer to the struct, trying to simplify the code for the example It is present in my code already

whatptr = &whatever;
whatever.what = 0;
// initialize both arrays to hold 0 at all indexes
// Then I must call a function progRun()
progRun();  //I need to pass struct 'whatever' in some way

Now progRun() looks like this:

void progRun(){
printWhat(&whatever);

    if (whatever.what == 0) {
    //do stuff 
    }
}

I can't change anything inside this code except what parameters to pass inside the progRun() function and I can add stuff before printWhat(). I've tried changing progRun to

void progRun(someType *stptr)

then calling it in main as

progRun(whatptr);

but this causes a stack overflow issue for some reason, I've tried stepping through with a debugger and it only occurs when the function is called. Is there a better way to pass the 'whatever' struct to the function so it can be passed into progRun() and printWhat() and can access 'whatever.what'?

Any help would be greatly appreciated! in the meantime I'll try to figure it myself if I can.

EDIT: Something else must be wrong in the code even though everything else has compiled and ran perfectly fine until this code was added. If I can break down the code and find out what's wrong I'll update the question. And no I cannot post the whole code as it is an assignment (this isn't the goal of the assignment trust me it focuses on data forwarding and more, just need to get this basic thing working) Thank you for help everyone.

EDIT: the MAX number used in the struct for something[MAX] and another[MAX] was extremely large ( I left my desktop that I started this project with back home, I'm currently using an old laptop that can't handle large arrays). All the answers below, and some of the stuff I used before now works fine.

void progRun(someStruct *ptr) {
    someStruct whatever2 = *ptr;
    printWhat(whatever2);
    if (whatever2.what == 0) { ...
}

whatptr = &whatever;
progRun(whatptr);

Your problem was that:

  • you need to pass a pointer to whatever , yet you were passing a variable ( whatptr ) that had absolutely nothing to do with whatever .

    You need to first assign the pointer to whatever into your pointer variable.

  • You are not dereferencing the pointer in the function

Alternately, get rid of pointer variables:

void progRun(someType *stptr) {
    printWhat(*stptr);
    if (stptr->what == 0) { ...
}

progRun(&whatever);

Instruction

someType whatever, *whatptr;

is the problem:

*whatptr will not point to the struct whatever unless you do the assignment as follows:

whatptr = &whatever;

Alternatively you could dynamically allocate memory on the heap for a pointer to your struct whatever by using the malloc() function and pass the pointer returned by malloc to the function progrun :

whatptr = (someType*) malloc ( sizeof(someType) );
if (whatptr == NULL) exit (1);

//whatever you need to do with your code

progrun(whatptr); // call function like this

In this case of course you will need to dereference your pointer to access member elements of the struct by using the arrow -> operator:

whatpr->what = 0; // for example

Also, check these tutorials to understand both approaches:


If you can't change print and if statements then you should pass your function a copy of your struct :

void progRun( someType whatever ){ // <---Passing by copy
printWhat(&whatever);

    if (whatever.what == 0) {
    //do stuff 
    }
}

and in your main() you should just call the function like this:

someType whatever;
//assign values to members of the struct
progRun(whatever);

and do not need at all to define and assign a pointer to the struct .

Though passing variables to functions by copy (especially when they are objects composed by many variables such as a struct is) is not a good behaviour:

  • it will require an overhead to copy all member elements
  • your copy will have a limited scope, which means that any change you do to the variable inside of the function will be lost when your function ends and will not be reflected on variable at main scope

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM