• C

Struct pointer not working?

Hi all:

==============
typedef struct{
      int getAmount;
}StorageStruct ;

int main(){
    StorageStruct *storage=NULL;

    //THIS WORKS FINE!!!
    //Assign storage array using malloc
    //Fill the array of structs

    numberOfItems(storage, numberOfStorages);
}

int numberOfItems(StorageStruct *storageList, int numberOfStorages){
    //Trying to access storageList just gives a runtime memory error:
    printf("%i", storageList[0].getAmount);
}

==============

What am I doing wrong??? :S
Sphinx5Asked:
Who is Participating?
 
PaulCaswellCommented:
A runtime memory error probably means 'storage' is either NULL or not a valid pointer. I'd suggest tghe problem is somewhere here:

>>    //Fill the array of structs

Sadly, I cannot debug this code. ;)

However, you would be safer to use the code:

if (0 < numberOfStorages)     printf("%i", storageList[0].getAmount);

Paul
0
 
Sjef BosmanGroupware ConsultantCommented:
Is this all the code there is? Probably not. If it were, you should be testing for a NULL-pointer.
0
 
Sphinx5Author Commented:
Yes testing for NULL is in there, I've removed code that I know works fine and error checking.
0
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

 
Sphinx5Author Commented:
Ok, numberOfStorages is pretty high, well above 0, but it's still giving a memory error.
0
 
PaulCaswellCommented:
Seriously, if this is all you are doing:

>>    //Assign storage array using malloc
>>    //Fill the array of structs

then

>>   printf("%i", storageList[0].getAmount);

would not fail with the error you are reporting. So, you must have a problem somewhere in:

>>    //Assign storage array using malloc
>>    //Fill the array of structs

Paul
0
 
Sjef BosmanGroupware ConsultantCommented:
I say its in the part //Assign storage array using malloc

Are you willing to show some more code, please?

Just a tip: use assert.h
0
 
Sphinx5Author Commented:
Hmm...well got it sorted. I don't have a clue what it was in the slightest! I think it was the way storageList was being used... Thanks anyway guys. points split between Paul and SJef in favour of Paul.

0
 
Sjef BosmanGroupware ConsultantCommented:
Like the old tele: you gave it a good bash? Hope it won't explode when you least expect it...

Sjef :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.