Very Very Simple C Question

Started by AGP, June 20, 2009, 07:27:27 PM

Previous topic - Next topic

AGP

I figure at the very least paulscode and Egon will know: what does the line "typedef struct animation Animation" do in a header file? I see no other defining reference to animation (lower or upper case) anywhere in this little assignment. I haven't done C programming in so long, I forgot more than I knew I had known. And to clarify, the assignment uses Animation* pointers a lot, I meant that this line is the only part that defines it.

paulscode

typedef by itself allows one to use a different name for a common var type.  For example, you could do something like this:
typedef int OOBERint;
And then you can create OOBERint's (which are just int's with a new name):
OOBERint x = 5;

"typedef struct" is similar to "typdef", except it is used for user-defined data types.  For example:
struct Data
{
    int x;
    int y;
};

In c, to create a variable of this type, you would use this:
struct Data d;

However, if you didn't want to have to keep typing "struct", the following simplifies things a bit:
struct data
{
    int x;
    int y;
};
typedef struct data Data;
Data d;

Or even:
typedef struct data
{
    int x;
    int y;
} Data;
Data d;


So for your example, it looks like the developer has created a data type he wants to reference as "Animation" rather than "struct animation" (i.e. less typing).

AGP

Got it, thanks a lot. But I would still, then have to put in

struct animation {
   variables
};


in my .c file from what I understand.

AGP

#3
Another quick pointer question. Given:

struct animation {//LIST
     char title[80];
     int lines;
     int columns;
     Frame* start; // (HEAD)
     Frame* end; // (TAIL)
};
struct frame {//NODE
     char* canvas;
     Frame* next;
};
typedef struct animation Animation;
typedef struct frame Frame;

How do I implement the following method, specifically assigning the title (allocated with []) to to its char* pointer?

Animation* createAnimation(char* title, int lin, int col) {
Animation a;
a.title = title;
a.lines = lin;
a.columns = col;
return &a;//THIS IS RIGHT, RIGHT?
}

paulscode

That seems to be correct.  Were you having problems accessing the data via the returned pointer?

AGP

a.title = title can't be right. It looks weird even to me and besides gcc has a problem with it. : -)

paulscode

Ah, ok I see that now (I was looking at the return &a part which is correct).  Would it be possible to change the animation struct so that the "title" there is a pointer instead of an array.  That way, you would allocate memory outside the struct and simply pass it a pointer.

Of course there is a way to use what you have now (i.e. reading chars from the pointer and placing them into the array), but it seems to have slipped my mind at the moment (I've been playing with Java too much lately, I guess).

AGP

Same here, pal! Java and C# rot our brains. : -) But no, unfortunately I have to use what I have. It's an assignment, and not even mine at that, but I'm trying to re-learn this so I can pass it on to my friend.

paulscode

Oh, I remember now.  You should be able to increment the pointer to grab the values.  Something like this:
for( int n = 0; n < 80; n++ )
{
    a.title[n] = *title;
    title++;
}

AGP

Man, C can be so weird! It now compiles, except for the fact that it tells me that I'm returning the address of a local variable (so I just put it outside the function but I wanted to mention it anyway). Thanks again, pal.

AGP

Oh, and while (cursor->next != NULL) is giving me a "NULL undeclared; first use in this function" message (same for lower-case). So what's the C equivalent? I think I do remember testing for 0 or something but a quick Google seems to have suggested I could use NULL. But gcc won't take it, so what's the equivalent?

AGP

Including stdio.h did it. So thanks again.

AGP

OK, here's another question: why doesn't
if(*cursor->canvas < strlen(cursor->canvas))
work? It seems to crash my program (no message, I just have to CTRL-C out of it).

paulscode

In this case, you are reading the first character of a string pointed to by a char*, and comparing that char to the length of the string.  The problem is probably related to type-casting, comparing a char to an int, or something along those lines (I'm actually surprised this compiles).  However, I assume that is not what you are trying to do here.  What do you want to figure out with this if statement?

AGP

#14
I thought this represented the index in the char array, or why would *cursor->canvas++ be possible? It must then be the char, and the compiler treats *cursor->canvas++ as a shortcut to increasing the index. What I needed ws to tell it not to go outside the bounds of the string, so I changed the code to keep track via a separate int.

EDIT: But the source of the crash had actually been strlen itself, since the string for a reason I hadn't seen was NULL.