typedef of array compiling problems in functions call

R

Rufus

Hi all,
I have a problem with the next example code. I can't compile it without
warnings with gcc.
The compilation out is:
$ gcc -Wall aPoint.c
aPoint.c: In function 'printaPoint2':
aPoint.c:23: warning: passing argument 1 of 'printaPoint1' from incompatible
pointer type
aPoint.c: In function 'printaPoint3':
aPoint.c:30: warning: passing argument 1 of 'printaPoint1' from incompatible
pointer type

and the call to printaPoint1 in printaPoint3 don't work, it shows strange
results
can someone help me to solve this doubt??

Thanks all

/**********************************aPoint.c*************************************/
/*gcc -Wall aPoint.c */
#include <stdio.h>
#include <stdlib.h>

typedef float aPoint[3];
//typedef struct {float a[3];}aPoint;

void printaPoint1(aPoint *pto)
{
int i;
//printf("printaPoint1: ptr=0x%X\n",&pto);
for(i=0;i<3;i++)
printf("%f ",(*pto));
printf("\n");
//for(i=0;i<3;i++) printf("%f ",(pto));printf("\n");

}

void printaPoint2(aPoint pto)
{
//printf("printaPoint2: ptr=0x%X &ptr=0x%X \n",pto,&pto);
printaPoint1(pto);

}

void printaPoint3(aPoint pto)
{
//printf("printaPoint2: ptr=0x%X &ptr=0x%X \n",pto,&pto);
printaPoint1(&pto);

}
int main()
{
aPoint pto;
pto[0]=1;
pto[1]=2;
pto[2]=3;
printf("call printaPoint1\n");
printaPoint1(&pto);
printf("call printaPoint2\n");
printaPoint2(pto);
printf("call printaPoint3\n");
printaPoint3(pto);
return 0;
}
~
 
F

Flash Gordon

Rufus wrote, On 01/07/07 21:24:
Hi all,
I have a problem with the next example code. I can't compile it without
warnings with gcc.
The compilation out is:
$ gcc -Wall aPoint.c

You should add a few more options.
gxx -ansi -pedantic -Wall -O aPoint.c
Adding either -W or -Wextra depending on your version would be useful as
well.
aPoint.c: In function 'printaPoint2':
aPoint.c:23: warning: passing argument 1 of 'printaPoint1' from incompatible
pointer type
aPoint.c: In function 'printaPoint3':
aPoint.c:30: warning: passing argument 1 of 'printaPoint1' from incompatible
pointer type

and the call to printaPoint1 in printaPoint3 don't work, it shows strange
results

Well, it would, seeing is your code is fundamentally broken.
can someone help me to solve this doubt??

Thanks all

/**********************************aPoint.c*************************************/
/*gcc -Wall aPoint.c */
#include <stdio.h>
#include <stdlib.h>

typedef float aPoint[3];
//typedef struct {float a[3];}aPoint;

void printaPoint1(aPoint *pto)
{

Here define printaPoint1 as taking a pointer to aPoint.

void printaPoint2(aPoint pto)
{
//printf("printaPoint2: ptr=0x%X &ptr=0x%X \n",pto,&pto);
printaPoint1(pto);

Here you pass an aPoint rather than a pointer to one. This is WRONG.
}

void printaPoint3(aPoint pto)
{
//printf("printaPoint2: ptr=0x%X &ptr=0x%X \n",pto,&pto);
printaPoint1(&pto);

<snip>

Here you are getting tripped up by your typedef and the fact that when
you attempt to specify a parameter as being an array, it is really just
a pointer.

Your code is such a mess I don't know what you are actually trying to
do, but whatever it is you are not trying to do it the right way. If you
tell us what you want to do as well as giving us your code people are
more likely to be able to point you in the right direction. However, I
suggest you start by reading section 6 of the comp.lang.c FAQ available
at http://c-faq.com/
 
O

Old Wolf

I have a problem with the next example code. I can't compile it without
warnings with gcc.

typedef float aPoint[3];

Many people prefer to not use array typedefs;
your code is a good example of the confusion
that can arise when they are used.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top