Pointers pointers.

C

Chad

I thought I could assign an array of chars to a pointer to pointer.
However, when I do
the following:

#include <stdio.h>

int main(void) {
char buff[] ="test";
char **output = &buff;

return 0;
}

I get:
$gcc -g ptr.c -o ptr
ptr.c: In function `main':
ptr.c:5: warning: initialization from incompatible pointer type

What am I doing wrong here?

Chad
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Chad said:
I thought I could assign an array of chars to a pointer to pointer.
However, when I do
the following:

#include <stdio.h>

int main(void) {
char buff[] ="test";
char **output = &buff;

return 0;
}

I get:
$gcc -g ptr.c -o ptr
ptr.c: In function `main':
ptr.c:5: warning: initialization from incompatible pointer type

The address of a char[5] is not a char **, it is a char (*)[5];
char (*output)[5] = &buff;
 
C

Chad

Nils said:
Chad said:
I thought I could assign an array of chars to a pointer to pointer.
However, when I do
the following:

#include <stdio.h>

int main(void) {
char buff[] ="test";
char **output = &buff;

return 0;
}

I get:
$gcc -g ptr.c -o ptr
ptr.c: In function `main':
ptr.c:5: warning: initialization from incompatible pointer type

The address of a char[5] is not a char **, it is a char (*)[5];
char (*output)[5] = &buff;

Perhaps this might be going beyond the limits of decent programming,
but would something like the following work:

#include <stdio.h>
#include <string.h>

int main(void) {
char buff[] ="test";

size_t len = strlen(buff) + 1;

char (*output)[len]=&buff;

return 0;
}
 
L

lovecreatesbea...

Chad said:
Nils said:
Chad said:
I thought I could assign an array of chars to a pointer to pointer.
However, when I do
the following:

#include <stdio.h>

int main(void) {
char buff[] ="test";
char **output = &buff;

return 0;
}

I get:
$gcc -g ptr.c -o ptr
ptr.c: In function `main':
ptr.c:5: warning: initialization from incompatible pointer type

The address of a char[5] is not a char **, it is a char (*)[5];
char (*output)[5] = &buff;

Perhaps this might be going beyond the limits of decent programming,
but would something like the following work:

#include <stdio.h>
#include <string.h>

int main(void) {
char buff[] ="test";

size_t len = strlen(buff) + 1;

char (*output)[len]=&buff;

char (*output)[sizeof buff]=&buff;
return 0;
}

#include <stdio.h>

int main(void) {
char buff[] = "test";
char *p = buff;

printf("%s\n", p);
return 0;
}
 
K

Keith Thompson

Chad said:
I thought I could assign an array of chars to a pointer to pointer.
However, when I do
the following:

#include <stdio.h>

int main(void) {
char buff[] ="test";
char **output = &buff;

return 0;
}

I get:
$gcc -g ptr.c -o ptr
ptr.c: In function `main':
ptr.c:5: warning: initialization from incompatible pointer type

What am I doing wrong here?

The comp.lang.c FAQ is at <http://www.c-faq.com/>. Read section 6,
"Arrays and Pointers".
 
L

lovecreatesbea...

Keith said:
Chad said:
I thought I could assign an array of chars to a pointer to pointer.
However, when I do
the following:

#include <stdio.h>

int main(void) {
char buff[] ="test";
char **output = &buff;

return 0;
}

I get:
$gcc -g ptr.c -o ptr
ptr.c: In function `main':
ptr.c:5: warning: initialization from incompatible pointer type

What am I doing wrong here?

The comp.lang.c FAQ is at <http://www.c-faq.com/>. Read section 6,
"Arrays and Pointers".

Hello Keith. The faq says:

Given an array a and pointer p, <snip> If you were to assign the
array's address to the pointer:
p = a;
then p[3] and a[3] would access the same element.

If I write code as follows, is it right?

char buff[] = "hello world";
char *p = buff;

Is a cast needed on the second line of code, i.e., should I write it as
follows:

char *p = (char *) buff;

Thank you.
 
F

Frederick Gotham

(e-mail address removed) posted:
Is a cast needed on the second line of code, i.e., should I write it as
follows:

char *p = (char *) buff;


No. The rule of thumb is:

Only use a cast if the code won't compile without one.

There are exceptions to this rule of course, such as using casts to suppress
compiler warnings:

int Func(long const val)
{
return (int)(val / 89);
}
 
L

lovecreatesbea...

Keith said:
The comp.lang.c FAQ is at <http://www.c-faq.com/>. Read section 6,
"Arrays and Pointers".

Hello Keith. The faq says:

Given an array a and pointer p, <snip> If you were to assign the
array's address to the pointer:
p = a;
then p[3] and a[3] would access the same element.

If I write code as follows, is it right?

char buff[] = "hello world";
char *p = buff;

Is a cast needed on the second line of code, i.e., should I write it as
follows:

char *p = (char *) buff;

In K&R2, sec 5.3, it writes the code like this without a cast. It seems
that the following code is right:

char *p = buff;
or
p = buff;

Is there a legal implicit conversion?
 
O

osmium

Frederick Gotham said:
No. The rule of thumb is:

Only use a cast if the code won't compile without one.

Sounds like a bad rule of thumb to me.

Consider:

double mean;
int sum;
int n;
.....
mean = sum/n;

Also casts can contribute to the documentation for people who must
subsequently diddle with the code.
 
L

lovecreatesbea...

Keith said:
The comp.lang.c FAQ is at <http://www.c-faq.com/>. Read section 6,
"Arrays and Pointers".

Hello Keith. The faq says:

Given an array a and pointer p, <snip> If you were to assign the
array's address to the pointer:
p = a;
then p[3] and a[3] would access the same element.

If I write code as follows, is it right?

char buff[] = "hello world";
char *p = buff;

Is a cast needed on the second line of code, i.e., should I write it as
follows:

char *p = (char *) buff;

In K&R2, sec 5.3, it writes the code like this without a cast. It seems
that the following code is right:

char *p = buff;
or
p = buff;

Is there a legal implicit conversion?

I understand it now. The array name in expression is a pointer to the
first element of the array. There is no implicit conversion and no need
to add a cast.
 
K

Keith Thompson

osmium said:
Sounds like a bad rule of thumb to me.

A rule of thumb, by definition, is not universal. The stated rule of
thumb is a very good one.
Consider:

double mean;
int sum;
int n;
....
mean = sum/n;

Yes, that's a case where a cast would be appropriate.
Also casts can contribute to the documentation for people who must
subsequently diddle with the code.

In such cases, it's usually better to leave the cast out and depend on
anyone reading the code to understand how C works.
 
C

CBFalconer

osmium said:
Sounds like a bad rule of thumb to me.

Consider:

double mean;
int sum;
int n;
....
mean = sum/n;

Bad example IMO. The above stores the truncated integral part from
the division, and may well be exactly what is desired. If this is
not desired, there are two possibilities:

a. mean = sum / (double)n;
b. mean = (double)sum / n;

and the point of each is to do the arithmetic in the floating point
processor, rather than the integer processor.

This is one area where Pascal avoids confusion, by having the /
operator for reals, and the DIV operator for integers. One more
argument against overloading of operators.
 
R

Richard Heathfield

Keith Thompson said:
A rule of thumb, by definition, is not universal. The stated rule of
thumb is a very good one.

I beg to differ. There are very few situations where a cast is a good idea,
but IIRC in most of those very few situations, omitting the cast will not
necessarily lead to a diagnostic message being issued. For example, to*,
is*, and the printfing of pointers.
Yes, that's a case where a cast would be appropriate.

<shrug>
mean = sum;
mean /= n;
</shrug>

Gain a cast to lose a line. Swings and roundabouts.
In such cases, it's usually better to leave the cast out and depend on
anyone reading the code to understand how C works.

Right. Trust the programmer, despite your instincts. :)
 
O

osmium

CBFalconer said:
Bad example IMO. The above stores the truncated integral part from
the division, and may well be exactly what is desired.

You must be much older than me. I have *never* wanted that result. To each
his own.
 
P

pete

Keith Thompson wrote:

The comp.lang.c FAQ is at <http://www.c-faq.com/>. Read section 6,
"Arrays and Pointers".

Hello Keith. The faq says:

Given an array a and pointer p, <snip> If you were to assign the
array's address to the pointer:
p = a;
then p[3] and a[3] would access the same element.

If I write code as follows, is it right?

char buff[] = "hello world";
char *p = buff;

Is a cast needed on the second line of code, i.e., should I write it as
follows:

char *p = (char *) buff;

In K&R2, sec 5.3, it writes the code like this without a cast. It seems
that the following code is right:

char *p = buff;
or
p = buff;

Is there a legal implicit conversion?

I understand it now. The array name in expression is a pointer to the
first element of the array.
There is no implicit conversion and no need to add a cast.

N869
6.3.2 Other operands
6.3.2.1 Lvalues and function designators

[#3] Except when it is the operand of the sizeof operator or
the unary & operator, or is a string literal used to
initialize an array, an expression that has type ``array of
type'' is converted to an expression with type ``pointer to
type'' that points to the initial element of the array
object and is not an lvalue.
 
L

lovecreatesbea...

Keith said:
A rule of thumb, by definition, is not universal. The stated rule of
thumb is a very good one.


Yes, that's a case where a cast would be appropriate.

Do you mean the casts for "sum" or "n"? It is because wrong data types
were used there. The types of "sum" or "n" should not be integer types,
they should be defined as float or double, etc., I think.
 
K

Keith Thompson

Do you mean the casts for "sum" or "n"? It is because wrong data types
were used there. The types of "sum" or "n" should not be integer types,
they should be defined as float or double, etc., I think.

It depends. If you want the average (as a real value) of a sequence
of integer values, it makes sense for sum and n to be integers, and
mean to be double.
 

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,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top