Solved

Struct pointer not working?

Posted on 2004-10-07
8
295 Views
Last Modified: 2010-04-15
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
Comment
Question by:Sphinx5
  • 3
  • 3
  • 2
8 Comments
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 12248313
Is this all the code there is? Probably not. If it were, you should be testing for a NULL-pointer.
0
 

Author Comment

by:Sphinx5
ID: 12248365
Yes testing for NULL is in there, I've removed code that I know works fine and error checking.
0
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 325 total points
ID: 12248718
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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:Sphinx5
ID: 12248888
Ok, numberOfStorages is pretty high, well above 0, but it's still giving a memory error.
0
 
LVL 16

Assisted Solution

by:PaulCaswell
PaulCaswell earned 325 total points
ID: 12249003
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
 
LVL 46

Assisted Solution

by:Sjef Bosman
Sjef Bosman earned 75 total points
ID: 12249509
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
 

Author Comment

by:Sphinx5
ID: 12250200
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
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 12250606
Like the old tele: you gave it a good bash? Hope it won't explode when you least expect it...

Sjef :)
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question