Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 332
  • Last Modified:

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
0
Sphinx5
Asked:
Sphinx5
  • 3
  • 3
  • 2
3 Solutions
 
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
 
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
A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

 
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.

Join & Write a Comment

Featured Post

Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now