1

I'm trying to pass a char array to a function in C.

My code is as follows

void Process(const char expect[])
{
    char c0 = expect[0];
    char c1 = expect[1];
}

Process("OK");

My debugger is telling me however that expect is a pointer to a single character, which doesn't make any sense to me. If I try to read the data from expect (what the c0 and c1 variables are doing), I get this (or something to this effect)

c0 = "O";
c1 = NULL; 

What confuses me further is that I've done a similar function using the same syntax for arguments that works without any issues. Any help would be appreciated - thanks in advance.

3
  • 2
    stackoverflow.com/help/mcve Commented Jul 7, 2018 at 17:51
  • I appreciate how to provide code, I'm more asking for the correct way to go about doing what I'm doing, and if there is anything obvious that I'm missing. Commented Jul 7, 2018 at 17:53
  • 1
    Superficially, if the code you show was the code your debugger was debugging, the debugger would be confused. However, since debuggers are nearly as reliable as compilers, that's unlikely to be the problem — that is, the debugger isn't wrong. The trouble is different from what you've sketched out. Hence, we need to see an MCVE (minimal reproducible example) — which needn't be very much bigger than what you've shown — add #include <stdio.h>, and printf("c0 = %c; c1 = %c\n", c0, c1); to Process(), and int main(void) { before the call to Process("OK"); and return 0; } after it. Then debug that! Commented Jul 7, 2018 at 17:58

2 Answers 2

1

Array passing is straightforward. Arrays don't get passed -- their innermost dimension decays to a pointer and void Process(char expect[]) is completely equivalent to void Process(char *expect).

Nothing in your (incomplete) example suggests it wouldn't work.

This

#include <stdio.h>
void Process(char expect[])
{
    char c0 = expect[0];
    char c1 = expect[1];
    printf("'%c' '%c'\n", c0, c1);
}
int main(void) { Process("OK"); }

must print 'O' 'K'. If it doesn't in the context of your program, then you must be doing something wrong or something in your program must be undefined.

Sign up to request clarification or add additional context in comments.

Comments

1

The simple answer is that that's just how C works: a pointer is a pointer to a single item, and that pointer can be incremented to point to the next item, and so on.

Additionally, arrays are always passed around as pointers to their first item. While they do have memory allocated for them at creation, they are from then on basically just pointers of the appropriate type.

Text strings in C are given special treatment: each visible symbol appears as a char in what is effectively an array of chars, and a zero indicates the end of the string.

1 Comment

Welcome to Stack Overflow. Please read the About and How to Answer pages soon(ish). Your comment would be better edited into your answer — it is useful information. You're allowed, even encouraged, to edit your answer to improve it. And when you've added to the answer, you can delete your comment and (I think) you can flag mine "no longer needed". I'll aim to remove it anyway after you've edited.

Your Answer

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.