Keep getting a warning.. Can someone please tell me why?

J

Jim

For some reason I keep getting the following error when compiling with
GCC. The program compiles and runs fine, but I get this error:

lab4b.c: In function 'main':
lab4b.c:47: warning: passing argument 1 of 'displayPtrArray' from
incompatible pointer type


** I'm doing this for a class that I'm taking so there might be a
better way to do it but I have to use an array of pointers.

-----------------------------
Below is my code:

#include <stdio.h>
#include <stdlib.h>
#define SIZE 10

// function prototypes
void displayArray(const double arr[], const int size);
void displayPtrArray(double const *arr[], const int size);

// main program
int main(void) {
double
x[SIZE] = {2.3, 5.66, 1.22, 5.6, 6.77, 3.2, 5.13, 2.82, 1, 5.01},
*xPtr[SIZE];

int cnt = 0;

// initialize the array with the pointer values
for(cnt = 0; cnt < SIZE; cnt++) {
xPtr[cnt] = &x[cnt];
}

// display array

printf("Orginal: ");
displayArray(x, SIZE);
printf("New : ");
displayPtrArray(xPtr, SIZE);

// sort the array

// display the original array again
}

void displayArray(double const arr[], const int size) {
int cnt = 0;

for(cnt = 0; cnt < size; cnt++)
printf("%5.2lf", arr[cnt]);

printf("\n\n");
}

void displayPtrArray(double const *arr[], const int size) {
int cnt = 0;

for(cnt=0; cnt < size; cnt++)
printf("%5.2lf", *arr[cnt]);

printf("\n\n");
}
 
E

Eric Sosman

Jim said:
For some reason I keep getting the following error when compiling with
GCC. The program compiles and runs fine, but I get this error:

lab4b.c: In function 'main':
lab4b.c:47: warning: passing argument 1 of 'displayPtrArray' from
incompatible pointer type

You've wandered into one of the odd and poorly-lit
corners of the language, which you can illuminate by
burning a copy of Question 11.10 of the comp.lang.c
Frequently Asked Questions (FAQ) list:

http://www.c-faq.com/

The dancing flames may or may not make you happy,
but they might keep you warm.
 
J

Jim

You've wandered into one of the odd and poorly-lit
corners of the language, which you can illuminate by
burning a copy of Question 11.10 of the comp.lang.c
Frequently Asked Questions (FAQ) list:

http://www.c-faq.com/

The dancing flames may or may not make you happy,
but they might keep you warm.

Are you basically saying not to worry about it?

What is it that I'm trying to do that is not ANSI compliant?? Is it
because I'm trying to pass in an array of pointers, or have I done
something wrong?
 
E

Eric Sosman

Jim said:
Are you basically saying not to worry about it?

No.
What is it that I'm trying to do that is not ANSI compliant?? Is it
because I'm trying to pass in an array of pointers, or have I done
something wrong?

Are you having trouble understanding what 11.10 says?
Try this: Rewrite your function arguments as explicit
pointers rather than as things that look like arrays (you're
probably aware that functions do not actually receive arrays
as arguments, but instead receive pointers to the arrays'
zeroth elements). This will make your code look just like
the code in 11.10; try re-reading 11.10 with your rewritten
code at hand, and see if it makes more sense this time.

(Why am I being so frustratingly obstinate about not just
doling out an Answer? Because you said you're writing the
code for a class, which tells me you are interested in learning.
That is a good thing -- and, I may say, a welcome contrast to
all those revolting "Do my homework d00d" losers! But the way
to learn something isn't to have an Answer fed to you and half-
understood, but for you to take a part in figuring it out for
yourself. Give a fish, or teach to fish. Happy fishing!)
 
J

Jim

Thanks very much!

I'm not interested in getting someone to do the work for me. I'm 30
years old and understand the difference from getting a grade and
passing to actually learning the material.

I did look at the FAQ but read 11.1 instead of 11.10 and it was
talking about being ANSI compliant, so I just misunderstood.

What you directed to me wasn't what I was doing (exactly), but I was
able to interpret it and adapt it for what I needed.

Thanks again!
 
E

Eric Sosman

Jim said:
Thanks very much!

I'm not interested in getting someone to do the work for me. I'm 30
years old and understand the difference from getting a grade and
passing to actually learning the material.

I did look at the FAQ but read 11.1 instead of 11.10 and it was
talking about being ANSI compliant, so I just misunderstood.

What you directed to me wasn't what I was doing (exactly), but I was
able to interpret it and adapt it for what I needed.

"Glad to be of ser-vice!" May your enthusiasm never flag.
 
T

Thad Smith

Jim said:
Thanks very much!

I'm not interested in getting someone to do the work for me. I'm 30
years old and understand the difference from getting a grade and
passing to actually learning the material.

I did look at the FAQ but read 11.1 instead of 11.10 and it was
talking about being ANSI compliant, so I just misunderstood.

Hmmm, when is 11.1 not equal to 11.10? If the difference is 0, they must
be the same!
 
C

christian.bau

It's a very subtle reason. What you do looks perfectly reasonable:
First, you have an array of doubles. You pass it to a function that is
not supposed to modify these doubles, so you declare the parameter as
"pointer to const double". That's fine. Next, you have an array of
pointers to doubles, you pass it to a function that is not supposed to
modify any of those doubles, so you make it an array of pointers to
const double. Seems fine, but it isn't. Here is why:

You should not be able to make an assignment to a const double without
using a cast (such an assignment won't work anyway, but the compiler
shouldn't allow you to do it by accident; if you use a cast like in
"const double x; double *p = (double *) &x; *p = 1.0; " then you
deserve whatever you get, so that is fine with the language.

Now with the function call that gave the warning, you can assign to a
const double without using a cast:

static const double my_const_double;

Inside the function displayPtrArray add an instruction: arr [0] =
&my_const_double;

After the call to displayPtrArray: *(xPtr [0]) = 1.0;

xPtr is an array of pointers to double, so you can freely store values
wherever these pointers point to. That means it would be dangerous to
store a pointer to const double into that array, and it cannot be done
without a cast. However, when you pass the array to displayPtrArray,
this dangerous thing is now possible without a cast.
 

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,773
Messages
2,569,594
Members
45,121
Latest member
LowellMcGu
Top