[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

How can I cast to an array type?

boltar2003

4/26/2011 9:28:00 AM

Hello

I want to use some code that has a pointer to an array type so I can use pointer
arithmetic on it, but I want to point it to a malloc'd char*. The code compiles
under gcc but with a warning about the assignment. My problem is I can't figure
out how to cast the char* to the array type to remove the warning.

eg:

char (*p)[100];
char *s;

p = s;

How would I write the cast so I could remove the warning from the "p = s" line?

Thanks for any help

B2003


5 Answers

jt

4/26/2011 9:41:00 AM

0

boltar2003@boltar.world wrote:
> I want to use some code that has a pointer to an array type so I can use
> pointer arithmetic on it, but I want to point it to a malloc'd char*. The
> code compiles under gcc but with a warning about the assignment. My problem
> is I can't figure out how to cast the char* to the array type to remove the
> warning. eg:

> char (*p)[100];
> char *s;
> p = s;

> How would I write the cast so I could remove the warning from the "p = s"
> line?

p = (char (*)[100]) s;

But then you could also assign the return value of malloc()
directly to 'p' without the need for any cast.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://t...

boltar2003

4/26/2011 9:56:00 AM

0

On 26 Apr 2011 09:40:51 GMT
jt@toerring.de (Jens Thoms Toerring) wrote:
>> How would I write the cast so I could remove the warning from the "p = s"
>> line?
>
>p = (char (*)[100]) s;

Thanks.

B2003

Ben Bacarisse

4/26/2011 10:31:00 AM

0

boltar2003@boltar.world writes:

> On 26 Apr 2011 09:40:51 GMT
> jt@toerring.de (Jens Thoms Toerring) wrote:
>>> How would I write the cast so I could remove the warning from the "p = s"
>>> line?
>>
>>p = (char (*)[100]) s;
>
> Thanks.

There's a simpler and more general way:

p = (void *)s;

All object pointers can be converted to void * and a void * can be
converted to a pointer of any object type. The first conversion is done
with a cast and the second conversion is done automatically by the
assignment. const can complicate the picture a little, but that's no a
problem is this case.

--
Ben.

ralph

4/26/2011 10:33:00 AM

0

On Tue, 26 Apr 2011 09:27:52 +0000 (UTC), boltar2003@boltar.world
wrote:

Or just
/* <comment on why> */
pragma Warnings ( Off, "<warning message>" )
p=s
pragma Warnings ( On, "<warning message>" )

-ralph
<g>

Keith Thompson

4/26/2011 3:37:00 PM

0

boltar2003@boltar.world writes:
> I want to use some code that has a pointer to an array type so I can use pointer
> arithmetic on it, but I want to point it to a malloc'd char*. The code compiles
> under gcc but with a warning about the assignment. My problem is I can't figure
> out how to cast the char* to the array type to remove the warning.
>
> eg:
>
> char (*p)[100];
> char *s;
>
> p = s;
>
> How would I write the cast so I could remove the warning from the "p = s" line?

You have a problem: the compiler is warning you about a conversion
between incompatible pointer types. You can add a cast to silence
the warning. Then you'll have two problems. The pointer types
are still incompatible, but the compiler won't complain because
you've silenced it. It's like taping over a warning light on your
car's dashboard.

malloc() returns a result of type void*, which can safely be assigned
directly to any pointer type (well, not to a pointer-to-function
type). Note that this isn't safe because it's of type void*,
it's safe because malloc() specifically returns a pointer value
that satisfies all possible alignment requirements.

You should also consider carefully whether you want a pointer to
an array. Arrays are usually manipulated via a pointer to the
element type, not to the array type. Incrementing such a pointer
steps through the elements of the array. Pointers to whole arrays
are mostly useful for multidimensional arrays (i.e., arrays where
the elements are themselves arrays).

The comp.lang.c FAQ at <http://c-fa... is an excellent resource.
Section 4 covers pointers; section 6 covers arrays and pointers.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"