0

How can i understand the code char*a[3]? Does it mean declaring an array which can hold addresses of three characters?

#include <stdio.h>

    int main(void)
    {
        char *a;
        scanf("%s",a); 
        printf("%s \n",a);
        return 0;
    }

The above code does not work gives the output

(null)

I understand it is because the the declared pointer is pointing to nothing Then I ran the following code:

#include <stdio.h>

    int main(void)
    {
        char *a[1];
        scanf("%s",a[0]); 
        printf("%s \n", a[0]);
        return 0;
    }

This one works perfectly and can take indefinite amount of string length. Why is that?

natzelo
  • 66
  • 7
  • 3
    The code invokes undefined behaviour, which means in your case, that it seems to work, but it is not guaranteed and will probably fail eventually. You have to provide a proper buffer of sufficient space to `scanf()` – Ctx Dec 06 '19 at 10:51
  • 2
    The above code does not work gives the output `(null)`...You are very lucky ;) – LPs Dec 06 '19 at 10:52
  • 1
    You want something like this: `char a[100]; scanf("%99s",a);`. BTW try your second program and provide a very long string as input. – Jabberwocky Dec 06 '19 at 10:53
  • 1
    @LPs interestingly enough, `scanf()` seems to refuse to write to a NULL pointer, thus avoiding a segmentation fault. This was new to me, afaik this is not specified and probably an implementation detail – Ctx Dec 06 '19 at 10:54
  • 1
    @Ctx It is new to me too. Moreover the variable is local and it is very rare to have it zeroed – LPs Dec 06 '19 at 10:57
  • 1
    It's a vector of pointers (3) to char – Math Lover Dec 06 '19 at 11:23

2 Answers2

6

This one works perfectly and can take indefinite amount of string length. Why is that?

Both of them are undefined behavior. It's just that undefined behavior isn't guaranteed to fail in any specific fashion, it can appear to work properly and then cause problems in different circumstances (which could include running the same program again on the same machine). On my machine, if I run this code, it crashes. The underlying issue is the same: a[0] is uninitialized and isn't pointing anywhere valid, just like a in the first program. Either way you'll have to allocate memory for the string that you're reading in:

int main(void)
{
    char *a[1];
    a[0] = malloc(20);
    scanf("%19s", a[0]);
    printf("%s \n", a[0]);
    free(a[0]);
    return 0;
}

Or without dynamical memory allocation:

int main(void)
{
    char a[20];
    scanf("%19s", a);
    printf("%s \n", a);
    return 0;
}

To answer the original question in the title: char*a[3] means "declare a as and array of 3 pointers to char".

Blaze
  • 16,736
  • 2
  • 25
  • 44
  • This does not answer the question that was explicitly asked, which is “What is the meaning of char *a[3]?” – Eric Postpischil Dec 06 '19 at 11:40
  • @EricPostpischil good catch. Sometimes I get lost in the details and forget what something was about. I added the answer to the question in the title. – Blaze Dec 06 '19 at 12:29
3

What is the meaning of char *a[3]? Does it mean declaring an array which can hold addresses of three characters?

Yes.

It's an array that holds three pointers. The pointers must be of type char *, meaning they reference a char.

char c1 = 'a';
char c2 = 'b';
char c3 = 'c';

char *a[3] = { &c1, &c2, &c3 };

printf("%c\n", *(a[0]));  // a

The char referenced by these pointers could be part of an array of characters, as is the case when they point to a character of a string.

char s1[] = "abc";
char s2[] = "def";
char s3[] = "ghi";

char *a[3] = { s1, s2, s3 };

printf("%s\n", a[0]);  // abc
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • This answer speaks to the question that was explicitly asked, “What is the meaning of char *a[3]?”, but it does not address why the first sample code printed “(null)” while the second sample code printed scanned characters. – Eric Postpischil Dec 06 '19 at 11:41
  • That is correct. Another answer already covered that. This should be obvious by the fact that both answers quote the question they answer – ikegami Dec 06 '19 at 11:45
  • @EricPostpischil @ikegami When I write `char *a = "hello"` , does `a` stores the address of the first byte of the string i.e. the address of `h` here? ? – natzelo Dec 06 '19 at 16:11
  • 1
    Yes. . . . . . . . – ikegami Dec 06 '19 at 21:54