Solved

Struct pointer not working?

Posted on 2004-10-07
8
311 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

726 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