Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

data structure problem 3

Status
Not open for further replies.

kreop

Technical User
Oct 9, 2005
14
JP
hai...This is a sample programming code and I failed to compile it. I guess the problem is {1,2,3}/3D koordinate (x,y,z)colored text

Code:
main()
{
	[COLOR=blue]struct[/color] hand{
		double finger_A;
		double finger_B;
		double finger_C;
	};

	[COLOR=blue]struct[/color] hand right_hand;
     
	right_hand.finger_A={1,2,3}; //The problem is here {1,2,3}


	return 0;

}

Please advise me Sir.
Tq.
 
Try this...
Code:
#include <stdio.h>

main()
{
    struct hand{
        double finger_A;
        double finger_B;
        double finger_C;
    };

/* Assigned during definition */

    struct hand right_hand = {1, 2, 3};

    printf("hand coords = %f, %f, %f\n",
        right_hand.finger_A, right_hand.finger_B, right_hand.finger_C);

/* Assigned one at a time */

    right_hand.finger_A = 4;
    right_hand.finger_B = 5;
    right_hand.finger_C = 6;

    printf("hand coords = %f, %f, %f\n",
        right_hand.finger_A, right_hand.finger_B, right_hand.finger_C);

/* Assigned together */

    right_hand = (struct hand) {7, 8, 9};

    printf("hand coords = %f, %f, %f\n",
        right_hand.finger_A, right_hand.finger_B, right_hand.finger_C);

    return 0;
}
 
Thank you SamBones . I tried to compile /*Assigned Together*/ but have some error.

Code:
#include <stdio.h>

main()
{
    struct hand{
        double finger_A;
        double finger_B;
        double finger_C;
    }right_hand;

    [COLOR=red]/* Assigned together */[/color]
    [COLOR=green]right_hand = (struct hand){7, 8, 9};//[COLOR=red]/Error here[/color][/color]

    printf("hand coords = %f, %f, %f\n",
        right_hand.finger_A, right_hand.finger_B,   right_hand.finger_C);

    return 0;
}

Tq
 
What was the error message? Always include the text of the error message.

What compiler are you using? It worked with Gnu C (gcc).
 
Maybe try this...
Code:
#include <stdio.h>

main()
{
    struct hand{
        double finger_A;
        double finger_B;
        double finger_C;
    } right_hand;

    /* Assigned together */
    right_hand = (struct hand) {7.0f, 8.0f, 9.0f};

    printf("hand coords = %f, %f, %f\n",
        right_hand.finger_A, right_hand.finger_B,   right_hand.finger_C);

    return 0;
}
Again, this compiles and runs on my GNU C.
 
No structure/array constants in C and C++, so assignment expression structure = {...} is wrong.
You may imitate (to some extent) structure constants, for example:
Code:
static const struct hand h1 = { 1, 2, 3 };
...
struct hand right_hand;
...
right_hand = h123;
...
or assign a structure value on member basis only:
Code:
right_hand.finger_A = 1.0;
...
Some compilers accept such structure constant assignments but it's a dangerous unportable feature. Aggregate value lists are to be found in initializers only, not in expressions.

It's interesting that you may write hand(1,2,3) etc constructs in C++ (if you add a proper constructor for hand structure). But it's another story.

Apropos, 7.0f is a float (not double) constant - don't use float type in (serious;) math calculations.
 
Yes.. still error . I am using Microsoft Visual C++ Ver.6.0. Three errors here.

Code:
 right_hand = (struct hand) {7.0f, 8.0f, 9.0f};
[COLOR=red]Error[/color]
1.Construction Error : '{'
2.Construction Error : ';' is needed before '{' 
3.Construction Error : ';' is needed before '}'
 
> right_hand = (struct hand) {7.0f, 8.0f, 9.0f};
This notation is a gcc-specific extension.
You can only (portably) initialise a struct variable with a list of values between braces.


--
 
tq for the comments and ideas.Actually I tried to develop a hirarachy structure of human hand using STRUCT function. The idea is finger_A, finger_B and finger_C are belong to STRUCT hand{...}. finger_A,B and C are 3D data {x,y,z}. My problem is how to express BELONG using C codes.

Code:
 struct hand{
        double right_hand;//(x0,y0,z0)
        double left_hand; //(x1,y1,z1)
	    }a;
	struct finger{
		double finger_A;//(x2,y2,z2)
		double finger_B; //(x3,y3,z3)
                 }b;

I tried to express finger_A is belong to right_hand using math subset as example A belong to B .

One more question,
What is the different between this 2 example codes.
Code:
 struct hand{
        double right_hand;//(x0,y0,z0)
        double left_hand; //(x1,y1,z1)
	[COLOR=blue]}a;[/color]
	struct finger{
		double finger_A;//(x2,y2,z2)
		double finger_B; //(x3,y3,z3)
                [COLOR=blue] }b;[/color]

and

Code:
 struct hand{
        double right_hand;//(x0,y0,z0)
        double left_hand; //(x1,y1,z1)

	struct finger{
		double finger_A;//(x2,y2,z2)
		double finger_B; //(x3,y3,z3)

                 [COLOR=blue]}b;[/color]
            [COLOR=blue]}a;[/color]

...in case of using pointer, does Struct hand{...}, structure members right_hand and left_hand has it own address?
 
Like this?
Code:
struct finger {
  double x;
  double y;
  double z;
};
struct hand {
  struct finger index;
  struct finger middle;
  struct finger ring;
  struct finger little;
};

Then two instances
Code:
struct hand left = {
  { 1, 2, 3 },
  { 1, 2, 3 },
  { 1, 2, 3 },
  { 1, 2, 3 },
};
struct hand right;
right.index.x = 1.0;

--
 
Tq. Yes. I have a question about the construction of structure.
for example,
Code:
struct finger {
		double x;
		double y;
		double z;
	};

	struct hand{

		[COLOR=red]struct finger thumb;[/color]
		struct finger index;
		struct finger middle;
		struct finger ring;
		};

In term of data what does it means by
Code:
        struct hand{
		[COLOR=red]struct finger thumb;[/color]
                };

how about this?? it function are same as pointer. struct hand is belong to struct hand_whole.I understand that content of struct hand is belong to struct hand_whole.
Code:
struct hand{

		struct finger thumb;
		struct finger index;
		struct finger middle;
		struct finger ring;
			};
	struct hand_whole{
		struct hand wrist;
		[COLOR=red]struct hand;[/color]
	};
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top