Solved

Struct pointer not working?

Posted on 2004-10-07
8
309 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
Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
why debugging a macro i s difficult 10 45
Pointer in one class to member in another 6 131
convert char array to number in c 5 93
Arduino EDI - Programming language 3 125
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

734 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