![]() |
|
#1
|
|||
|
|||
typedef struct problemHi.
How do I initialize this? struct pixmap{ unsigned char *p; int x; int y; int bpp; }; typedef struct pixmap pix; pix *p; |
|
#2
|
||||
|
||||
|
First off when you post code use c code tages that is put [ C ] (with no spaces) before your code and [ / C ] after your code (again no spaces).
Now to your question. What do you mean how do you initilize it? do you mean how can you assign values to your struct? if so thats easy simply do this: CPP / C++ / C Code:
|
|
#3
|
|||
|
|||
|
Thank you for your reply.
When I do it like you did it gives the following errors: error C2228: left of '.x' must have class/struct/union type If I put -> instead of . it copmiles with no errors. But when the program starts it gives me this: Run-Time Check Failure #3 - The variable 'p' is being used without being defined. Here is what I'm trying to do: CPP / C++ / C Code:
|
|
#4
|
|||
|
|||
|
you have a variable unsigned char *p inside your structure, and then you have an object declared as pix *p. change one of those to some other name. also use parenthesis inside your for loop: [(x+y)*(p->w)]
__________________
spasms!!! |
|
#5
|
|||
|
|||
SuggestionsQuote:
Well, i dont think its because "you have a variable unsigned char *p inside your structure, and then you have an object declared as pix *p" as machinated said because you can have the same variable name for a struct or class and a variable inside it. I think your problem is that you are only creating a pointer of type pix: pix *p; but you arent initializing it... it should point to a variable of type pix before you can asign a value to its elements... Also i would suggest as machinated said that you check and put parenthesis inside here: [x+y*p->w] what you want? this: [x+(y*p->w)] or this [(x+y)*p->w] ?? The other parts of your code i think are right. |
|
#6
|
||||
|
||||
|
why I think sho is right about not needing to chang pix *p to another name it would be good coding technique. The problem, I believe is that your making a pointer of type pix which is a type of struct so your making a pointer to a struct type variable instead of a pointer to a struct. If you keep your code the way it is you would have to put:
CPP / C++ / C Code:
CPP / C++ / C Code:
and your code should work out but with this you may actually need to change *p to another name to not get an error |
|
#7
|
|||
|
|||
|
Quote:
How can I do that? Right now the program compiles without any errors. But it points me to "(*(*p)).x = width;" and says "Run-Time Check Failure #3 - The variable 'p' is being used without being defined". I tried doing this: CPP / C++ / C Code:
It gives me "0xC0000005: Access violation reading location 0x00000000" |
|
#8
|
|||
|
|||
|
Quote:
you're assigning the pointer to NULL. you can't assign it values when it's pointing at NULL. you must allocate it memory first and then assign. pix *p = new pix; p->x = width; __________________
spasms!!! |
|
#9
|
|||
|
|||
|
Thats right, when you do pix *p; you are declaring a pointer to a variable of type pix but you arent initializing it...
you have to make it to point to a pix type element and to do that you have to create it and allocate memory for it with the new operator like machinated said: CPP / C++ / C Code:
when you use the NULL keyword with a pointer it means the pointer isnt pointing to any element... and is used to "deactivate" a pointer which in the past pointed to some element that isnt disponible now... that way you can prevent problems from your program trying to access an unavailabe memory location... |
Recent GIDBlog
Developing GUIs with wxPython (Part 3) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Urgent ! Pls Help Me ! | mycashmoney | C Programming Language | 4 | 01-Jul-2006 22:49 |
| strange sizeof(structure) - multiple of 8 | pinkpanther | C Programming Language | 11 | 30-May-2004 07:20 |
| problem:retrieve from struct | kelly80 | C Programming Language | 6 | 29-Apr-2004 07:49 |
| reading a char* into struct data | spike666 | C Programming Language | 7 | 19-Apr-2004 12:06 |
| Another FX 5600 problem (but with details that might shed light on this) | BobDaDuck | Computer Hardware Forum | 2 | 16-Apr-2004 07:53 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The