GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Jun-2004, 14:14
grizli grizli is offline
New Member
 
Join Date: Jun 2004
Posts: 7
grizli is on a distinguished road
Unhappy

typedef struct problem


Hi.
How do I initialize this?

struct pixmap{
unsigned char *p;
int x;
int y;
int bpp;
};
typedef struct pixmap pix;
pix *p;
  #2  
Old 18-Jun-2004, 16:24
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 165
dabigmooish will become famous soon enough
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:

typedef struct pixmap p;

p.x = 10;
p.bpp = 13;

its that easy.
  #3  
Old 18-Jun-2004, 17:23
grizli grizli is offline
New Member
 
Join Date: Jun 2004
Posts: 7
grizli is on a distinguished road
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:
...

COLORREF color;

struct pixmap{
   unsigned char *p;
   int w;
   int h;
   int bpp;
};
typedef struct pixmap pix;

...

pix *p;

p->w = width;
p->h = height;
p->bpp = 3;
for (int y=0;y<p->h;y++)
   for(int x=0;x<p->w;x++){
      color = getcolor(x,y);
      p->p[x+y*p->w] = filter(color);
   }
}
  #4  
Old 18-Jun-2004, 17:36
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
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  
Old 18-Jun-2004, 18:19
sho sho is offline
Junior Member
 
Join Date: Jun 2004
Posts: 49
sho will become famous soon enough

Suggestions


Quote:
Originally Posted by grizli
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:
...

COLORREF color;

struct pixmap{
   unsigned char *p;
   int w;
   int h;
   int bpp;
};
typedef struct pixmap pix;

...

pix *p;

p->w = width;
p->h = height;
p->bpp = 3;
for (int y=0;y<p->h;y++)
   for(int x=0;x<p->w;x++){
      color = getcolor(x,y);
      p->p[x+y*p->w] = filter(color);
   }
}

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  
Old 19-Jun-2004, 00:06
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 165
dabigmooish will become famous soon enough
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:
*(p)->w = width //this is actually (*(*P).w)) = width
All you need to do is
CPP / C++ / C Code:
typedef struct pixmap *p; 

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  
Old 19-Jun-2004, 05:13
grizli grizli is offline
New Member
 
Join Date: Jun 2004
Posts: 7
grizli is on a distinguished road
Quote:
but you arent initializing it... it should point to a variable of type pix before you can asign a value to its elements...

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:
pix *p = NULL;
(*(*p)).x = width;

It gives me "0xC0000005: Access violation reading location 0x00000000"
  #8  
Old 19-Jun-2004, 07:30
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
Quote:
Originally Posted by grizli
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:
pix *p = NULL;
(*(*p)).x = width;

It gives me "0xC0000005: Access violation reading location 0x00000000"

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  
Old 19-Jun-2004, 16:32
sho sho is offline
Junior Member
 
Join Date: Jun 2004
Posts: 49
sho will become famous soon enough
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:
pix *p = new pix; // declaration of pointer and initialization to a new pix variable
p->x = width; // now you can access the elements of your new struct

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 GIDBlogDeveloping GUIs with wxPython (Part 3) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 20:40.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.