i am facing several problems....

first one is: how do i typedef a structure
i've seen many ways to do it but im not quite sure which one is the right one.
this is what i think how it goes (but dont know why it goes that way)

typedef struct MyStucture mystructure;
struct MyStructure
{
   ....
} abc[10];

and i dont know what is identifier, and what is object here...at least - im not quite sure

second problem is when i try to make a swap function:

void swapAb (mystructure *a, mystructure *b)
{
    mystructure *temp;
    temp = x;
    x = y;
    y = temp;
}

- it doesnt work, no compiler errors but no swap is made
that would be for now
thanks in advance

Recommended Answers

All 5 Replies

>how do i typedef a structure
The method I usually see is a compact one:

typedef struct foo {
  /* ... */
} foo;

But you can do it your way as well (though note you have a spelling error in the typedef; it's MyStructure, not MyStucture ;)). FYI, the structure tag name space and the typedef name space are separate, which is why you can use the same name for the structure and the typedef.

>and i dont know what is identifier, and what is object here
MyStructure is an identifier, as is mystructure. When you create an instance of the structure, that's an object:

struct MyStructure foo; /* foo refers to an object */
mystructure bar; /* bar refers to an object */

>it doesnt work, no compiler errors but no swap is made
Erm, the code is pretty wrong. You don't even touch a or b, x and y don't exist unless they're global, and you want to swap the contents of the pointer, not the pointers themselves:

void swap ( mystructure *a, mystructure *b )
{
  mystructure temp = *a;
  *a = *b;
  *b = temp;
}

i wanted to write:

void swap ( mystructure *a, mystructure *b )
{
    mystructure *temp = a;
    a = b;
    b = temp;
}

but my head was going to blow since im so tired
i actually wanted to swap pointers but i forgot that they doesnt even exist because i call function as: swap (&x, &y) i think i get it, at least some of it :D

one more question:
in your definition of structure (one with typedef) you didnt declere variables, if i wanted to make some i would type foo arrayOfTypeStructFoo[10]; somewhere later
am i right?

>am i right?
Yes.

thanks, this is solved :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.