array of pointers

i think i have declared an array of pointers and i believe i have initialized ar[1] pointer to a valid memory spot aka ar[1]=&a; i am struggling to understand where i get it wrong. Please help me see my mistake.

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>

int main()
{
    char a, *ar[2];
    
    a="hello people";
    ar[1]=&a;
    printf("a=%s",ar[1]);
}
How can char a = "hello people";? It is only one char.
The syntax of line 8 is good though.
The assumption printf makes when displaying strings is that they are null terminated. This is how the c stdio library knows when it is at the end of a string. Since memory in RAM can be a bit random, you want to make sure that strings are null terminated when they enter printf.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>

int main()
{
    char a[] = "hello people";
    char b[] = "hello other people";

    char *ar[2];
    ar[0] = &(a[0]); //the () are not necessary here
    ar[1] = &(b[0]); //but it makes it easier to read

    printf("a=%s\n", ar[0]);
    printf("b=%s\n", ar[1]);
}

http://ideone.com/AbrbFG
Topic archived. No new replies allowed.