Author Topic: Very Very Simple C Question  (Read 7768 times)

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Very Very Simple C Question
« on: June 20, 2009, 07:27:27 pm »
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.

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: Very Very Simple C Question
« Reply #1 on: June 20, 2009, 11:02:17 pm »
typedef by itself allows one to use a different name for a common var type.  For example, you could do something like this:
Code: [Select]
typedef int OOBERint;And then you can create OOBERint's (which are just int's with a new name):
Code: [Select]
OOBERint x = 5;
"typedef struct" is similar to "typdef", except it is used for user-defined data types.  For example:
Code: [Select]
struct Data
{
    int x;
    int y;
};
In c, to create a variable of this type, you would use this:
Code: [Select]
struct Data d;
However, if you didn't want to have to keep typing "struct", the following simplifies things a bit:
Code: [Select]
struct data
{
    int x;
    int y;
};
typedef struct data Data;
Data d;
Or even:
Code: [Select]
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).

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Very Very Simple C Question
« Reply #2 on: June 20, 2009, 11:52:46 pm »
Got it, thanks a lot. But I would still, then have to put in
Code: [Select]
struct animation {
    variables
};

in my .c file from what I understand.

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Very Very Simple C Question
« Reply #3 on: June 21, 2009, 12:28:42 am »
Another quick pointer question. Given:
Code: [Select]
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?
Code: [Select]
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?
}
« Last Edit: June 21, 2009, 01:22:02 am by AGP »

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: Very Very Simple C Question
« Reply #4 on: June 21, 2009, 01:27:00 am »
That seems to be correct.  Were you having problems accessing the data via the returned pointer?

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Very Very Simple C Question
« Reply #5 on: June 21, 2009, 01:28:51 am »
a.title = title can't be right. It looks weird even to me and besides gcc has a problem with it. : -)

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: Very Very Simple C Question
« Reply #6 on: June 21, 2009, 02:23:34 am »
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).

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Very Very Simple C Question
« Reply #7 on: June 21, 2009, 02:33:37 am »
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.

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: Very Very Simple C Question
« Reply #8 on: June 21, 2009, 04:00:35 am »
Oh, I remember now.  You should be able to increment the pointer to grab the values.  Something like this:
Code: [Select]
for( int n = 0; n < 80; n++ )
{
    a.title[n] = *title;
    title++;
}

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Very Very Simple C Question
« Reply #9 on: June 21, 2009, 04:07:55 am »
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.

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Very Very Simple C Question
« Reply #10 on: June 21, 2009, 04:21:13 am »
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?

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Very Very Simple C Question
« Reply #11 on: June 21, 2009, 04:25:14 am »
Including stdio.h did it. So thanks again.

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Very Very Simple C Question
« Reply #12 on: June 22, 2009, 12:52:55 am »
OK, here's another question: why doesn't
Code: [Select]
if(*cursor->canvas < strlen(cursor->canvas))work? It seems to crash my program (no message, I just have to CTRL-C out of it).

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: Very Very Simple C Question
« Reply #13 on: June 22, 2009, 02:18:27 am »
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?

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Very Very Simple C Question
« Reply #14 on: June 22, 2009, 03:30:11 am »
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.
« Last Edit: June 22, 2009, 07:36:33 am by AGP »