How to declare a pointer to a character array in C?
Asked Answered
B

3

5

How do I declare a pointer to a character array in C?

Bushnell answered 19/3, 2012 at 23:30 Comment(7)
I'm tempted to say something snarky like "Read the assigned chapter"... in any event, you need more information: language? overall objective? context? This doesn't seem to meet the bar of expectation of questions on this site.Drifter
Do you really want a pointer to an array? Or a pointer to the first element of an array? Do you know about cdecl?Phocine
To expand a bit on Carl's point: a pointer to an array is possible, but rarely (oh, so very rarely) needed or wanted. Most of the time, you want a pointer to (often const) char.Unsuspected
Thanks for answering @CarlNorum , the funny part is I was expecting the respective page from cplusplus.com, but this outranked it, which is good because know I know about cdecl!Delaminate
@JerryCoffin "~rarely (oh, so very rarely) needed or wanted" are you talking in the C++ application or C? I thought int main (int argc, char** argv) is common in the C (char** argv is an example of the pointer to the array).Confession
@CloudCho: that's a pointer to a pointer, which happens to refer to the initial element of an array--but it still doesn't have type "pointer to array".Unsuspected
The pointer isn't the array of character, for example, argv[0] = "main.cpp" in C language?Confession
P
11

I guess I'll give this answer in parts:

  1. Here's a pointer to an array of chars (I assumed a 10-element array):

    char (*x)[10];
    

    Let's break it down from the basics:

    x
    

    is a pointer:

    *x
    

    to an array:

    (*x)[10]
    

    of chars:

    char (*x)[10]
    
  2. However, most of the time you don't really want a pointer to an array, you want a pointer to the first element of an array. In that case:

    char a[10];
    char *x = a;
    char *y = &a[0];
    

    Either x or y are what you're looking for, and are equivalent.

  3. Tip: Learn about cdecl to make these problems easier on yourself.

Phocine answered 19/3, 2012 at 23:34 Comment(3)
@Carl I think you've missed some asterisks in item 2Arteriosclerosis
Yup - most definitely. Thanks, @sidyll! If you see me do that in the future, feel free to edit them in yourself - it seems you have plenty of rep to do so.Phocine
Well, I just didn't want to break the integrity of the answer from a user for whom I have great respect!Arteriosclerosis
C
3

You can declare it as extern char (*p)[];, but it's an incomplete type. This is of course because C has no "array" type generically that is a complete type; only arrays of a specific size are complete types.

The following works:

extern char (*p)[];

char arr[20];

char (*p)[20] = &arr;  // complete type now: p points to an array of 20 chars
Chaqueta answered 19/3, 2012 at 23:35 Comment(0)
C
0

I could suggest...

Declaration is:
char* array;

And then definition is:
array = "James Bond";

To print out:
printf ("%s \n", array);

Some programmers probably mention the above code isn't memory safe or the definition is actually re assignment, so

char* array = "James Bond";

would be a general practice.

Confession answered 1/3 at 23:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.