How can I cast to an array type?

B

boltar2003

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
 
J

Jens Thoms Toerring

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
 
R

ralph

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

-ralph
<g>
 
K

Keith Thompson

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-faq.com/> is an excellent resource.
Section 4 covers pointers; section 6 covers arrays and pointers.
 

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,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top