1

I have a pointer array defined declared as

char (*c)[20]

When allocating memory using malloc

c=malloc(sizeof(char)*20);

or

c=(char*)malloc(sizeof(char)*20);

I get a warning as "Suspicious pointer conversion"

Why?

pmr
60k11 gold badges119 silver badges161 bronze badges
asked Jan 16, 2012 at 15:23
13
  • 2
    Why would you be using malloc there? c is already initialized because it is an array. Commented Jan 16, 2012 at 15:25
  • 1
    If you just want a pointer to 20 chars, chances are you just want char *c=malloc(20);. Commented Jan 16, 2012 at 15:27
  • no, using size of (c) gets the pointer size Commented Jan 16, 2012 at 15:27
  • 3
    @RichardJ.RossIII: No, c is just a pointer, and uninitialized. Commented Jan 16, 2012 at 15:28
  • @Alex K. Please try to avoid small typo edits and fix the whole post. I lost my changes and had to redo them. That could have saved everybody some work. Commented Jan 16, 2012 at 15:28

5 Answers 5

5

In this declaration

char (*c)[20];

c object has type char (*)[20].

We know that in C malloc return type is void * and that there is an implicit conversion between void * to any object pointer types.

So c = malloc(whatever_integer_expression) is valid in C. If you get a warning, you are probably using a C++ compiler or you are using a C compiler but forgot to include the stdlib.h standard header.

But c = (char*) malloc(whatever_integer_expression) is not valid C because there is no implicit conversion between char * type and char (*)[20] type. The compiler has to (at least) warn.

answered Jan 16, 2012 at 15:32
Sign up to request clarification or add additional context in comments.

Comments

1

First of all, make sure you have stdlib.h included.

Secondly, try rewriting it as

c = malloc(sizeof *c);

I suspect you're getting the diagnostic on the second case because char * and char (*)[20] are not compatible types. Don't know why the first case would complain (at compile-time, anyway) unless you don't have stdlib.h included.

edit

Remember that you will have to dereference the pointer before applying the subscript; that is, your expressions will have to be

(*c)[i] = val;
printf("%c", (*c)[j]);

etc.

Alternately you could write c[0][i] in place of (*c)[i], but that's probably more confusing if c isn't supposed to act like a 2-d array.

answered Jan 16, 2012 at 15:29

Comments

1

Because you are defining C as a pointer to a static array of chars, not as a pointer to an array of chars, that is, a pointer to the first char.

Change

char (*c)[20];

for

char * c;

a

answered Jan 16, 2012 at 15:30

1 Comment

No, c is defined as a pointer to an array of 20 char. Array of pointers to char would be without the parens.
0

For an array of 20 characters, counting the NUL terminator, you don't need a pointer

char array[20];

for a pointer to char, you don't need an array

char *pointer;

A pointer to char can point to an array

pointer = array;

to part of the array (assuming no 'funny' business with the NUL terminator)

pointer = &array[10];

or to a bunch of bytes allocated with malloc

pointer = malloc(20);
answered Jan 16, 2012 at 15:40

Comments

0

just use as

c=(char(*)[20])malloc(sizeof(char)*20);

Reason:

1, as @ouah said.

2, in K&R style compiler, the above one is valid and expected.

answered Mar 6, 2013 at 15:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.