f



Pointers pointers.

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

0
cdalten (975)
9/8/2006 12:43:53 PM
comp.lang.c 30656 articles. 5 followers. spinoza1111 (3246) is leader. Post Follow

16 Replies
761 Views

Similar Articles

[PageSpeed] 50

Chad wrote:
> 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;
0
NOS (445)
9/8/2006 12:55:26 PM
Nils O. Sel=E5sdal wrote:
> Chad wrote:
> > 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[] =3D"test";
> >     char **output =3D &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] =3D &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[] =3D"test";

    size_t len =3D strlen(buff) + 1;

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

    return 0;
}

0
cdalten (975)
9/8/2006 1:13:07 PM
Chad wrote:
> Nils O. Sel=E5sdal wrote:
> > Chad wrote:
> > > 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[] =3D"test";
> > >     char **output =3D &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] =3D &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[] =3D"test";
>
>     size_t len =3D strlen(buff) + 1;
>
>     char (*output)[len]=3D&buff;

      char (*output)[sizeof buff]=3D&buff;

>     return 0;
> }

#include <stdio.h>

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

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

0
9/8/2006 2:52:31 PM
"Chad" <cdalten@gmail.com> writes:
> 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".

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
We must do something.  This is something.  Therefore, we must do this.
0
kst-u (21963)
9/8/2006 8:06:32 PM
Keith Thompson wrote:
> "Chad" <cdalten@gmail.com> writes:
> > 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.

0
9/9/2006 2:34:38 PM
lovecreatesbea...@gmail.com 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);
    }

-- 

Frederick Gotham
0
fgothamNO (1668)
9/9/2006 2:49:21 PM
lovecreatesbea...@gmail.com wrote:
> 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?

0
9/9/2006 2:52:30 PM
"Frederick Gotham" writes:

> 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. 


0
r124c4u1022 (2303)
9/9/2006 2:59:45 PM
lovecreatesbea...@gmail.com wrote:
> lovecreatesbea...@gmail.com wrote:
> > 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.

0
9/9/2006 3:25:15 PM
"osmium" <r124c4u102@comcast.net> writes:
> "Frederick Gotham" writes:
>> 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.

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.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
We must do something.  This is something.  Therefore, we must do this.
0
kst-u (21963)
9/9/2006 6:06:10 PM
osmium wrote:
> "Frederick Gotham" writes:
> 
>> 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;

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.

-- 
Chuck F (cbfalconer at maineline dot net)
   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>


-- 
Posted via a free Usenet account from http://www.teranews.com

0
cbfalconer (19194)
9/9/2006 7:02:56 PM
Keith Thompson said:

> "osmium" <r124c4u102@comcast.net> writes:
>> "Frederick Gotham" writes:
>>> 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.
> 
> 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.

> 
>> Consider:
>>
>> double mean;
>> int sum;
>> int n;
>> ....
>> mean = sum/n;
> 
> 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.

>> 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.

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

-- 
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
0
invalid171 (7008)
9/9/2006 7:16:32 PM
"CBFalconer" writes:

> osmium wrote:
>> "Frederick Gotham" writes:
>>
>>> 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;
>
> 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.


0
r124c4u1022 (2303)
9/9/2006 7:50:53 PM
lovecreatesbea...@gmail.com wrote:
> 
> lovecreatesbea...@gmail.com wrote:
> > lovecreatesbea...@gmail.com wrote:
> > > 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.

-- 
pete
0
pfiland (6613)
9/9/2006 11:35:11 PM
Keith Thompson wrote:
> "osmium" <r124c4u102@comcast.net> writes:
> > "Frederick Gotham" writes:
> >> 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.
>
> 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.

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.

0
9/10/2006 1:41:43 AM
"lovecreatesbea...@gmail.com" <lovecreatesbeauty@gmail.com> writes:
> Keith Thompson wrote:
>> "osmium" <r124c4u102@comcast.net> writes:
[...]
>> > Consider:
>> >
>> > double mean;
>> > int sum;
>> > int n;
>> > ....
>> > mean = sum/n;
>>
>> 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.

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.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
We must do something.  This is something.  Therefore, we must do this.
0
kst-u (21963)
9/10/2006 4:40:11 AM
Reply: