pointer array problem?


B

Bill Cunningham

I read and read my text and; well I don't know what going on here. I
have tried several things. I have to lower myself to asking what's wrong.
Maybe I'll learn something. This wouldn't even compile. All the different
things I tried gave all the same errors.

#include <stdio.h>

int main()
{
char p[3] = { "one", "two", "three" };
char *pa[3];
pa[0] = &p[2];
pa[1] = &p[1];
pa[2] = &p[0];
printf("%s %s %s\n", p[0], p[1], p[2]);
printf("%s %s %s\n", *(pa[0]), *(pa[1]), *(pa[2]));
return 0;
}

p.c: In function 'main':
p.c:5:5: error: excess elements in char array initializer
p.c:5:5: error: (near initialization for 'p')
p.c:5:5: error: excess elements in char array initializer
p.c:5:5: error: (near initialization for 'p')
p.c:5:17: warning: initializer-string for array of chars is too long
[enabled by default]

I guess the problems is on line 5.

Bill
 
Ad

Advertisements

X

Xavier Roche

Le 18/05/2014 06:58, Bill Cunningham a écrit :
char p[3] = { "one", "two", "three" };

char p[3] is an array of char, and may only contain 3 "char", including
the terminating \0 (such as the string "hi")

did you mean char* p[3] ?

or, better,
const char* p[3] = { ... };

(string literals are constant, because even if their type is char[],
modifying it is "undefined" and segfaults on modern systems)

you are probably confusing "char" and "string". char is a (generally
8-bit) character.
 
B

Borax Man

Xavier said:
Le 18/05/2014 06:58, Bill Cunningham a écrit :
char p[3] = { "one", "two", "three" };

char p[3] is an array of char, and may only contain 3 "char", including
the terminating \0 (such as the string "hi")

did you mean char* p[3] ?

or, better,
const char* p[3] = { ... };

(string literals are constant, because even if their type is char[],
modifying it is "undefined" and segfaults on modern systems)

you are probably confusing "char" and "string". char is a (generally
8-bit) character.


The "char p" section is an array of pointers to string literals. You don't
need to declare the number of elements, but you DO need to specify it is a
pointer (add the "*").

Then it is just a matter of making the array of pointers "pa", point to the
same string as pointer p. Remember, we want the address of the string, not
the address of the pointer, so don't add "&".


#include <stdio.h>

int main()
{
char *p[] = { "one", "two", "three" }; /* 3 pointers po
char *pa[3];
pa[0] = p[2];
pa[1] = p[1];
pa[2] = p[0];
printf("%s %s %s\n", p[0], p[1], p[2]);
printf("%s %s %s\n", pa[0], pa[1], pa[2]);
return 0;
}
 
B

Bir lau

I read and read my text and; well I don't know what going on here. I

have tried several things. I have to lower myself to asking what's wrong.

Maybe I'll learn something. This wouldn't even compile. All the different

things I tried gave all the same errors.



#include <stdio.h>



int main()

{

char p[3] = { "one", "two", "three" };

char *pa[3];

pa[0] = &p[2];

pa[1] = &p[1];

pa[2] = &p[0];

printf("%s %s %s\n", p[0], p[1], p[2]);

printf("%s %s %s\n", *(pa[0]), *(pa[1]), *(pa[2]));

return 0;

}



p.c: In function 'main':

p.c:5:5: error: excess elements in char array initializer

p.c:5:5: error: (near initialization for 'p')

p.c:5:5: error: excess elements in char array initializer

p.c:5:5: error: (near initialization for 'p')

p.c:5:17: warning: initializer-string for array of chars is too long

[enabled by default]



I guess the problems is on line 5.



Bill

Borax Man may give the right method that you want. I want to point that
you just declare the char * array pa and you have not allocate memory space for it in your program.
 
B

Bill Cunningham

Xavier Roche said:
Le 18/05/2014 06:58, Bill Cunningham a écrit :
char p[3] = { "one", "two", "three" };

char p[3] is an array of char, and may only contain 3 "char", including
the terminating \0 (such as the string "hi")

did you mean char* p[3] ?

or, better,
const char* p[3] = { ... };

(string literals are constant, because even if their type is char[],
modifying it is "undefined" and segfaults on modern systems)

you are probably confusing "char" and "string". char is a (generally
8-bit) character.

Oh yes yes. I did. Where is my head. Yes I meant a string literal not a
single char.
 
B

Bill Cunningham

[snip]
Then it is just a matter of making the array of pointers "pa", point to
the
same string as pointer p.

* Remember, we want the address of the string, not
the address of the pointer, *

so don't add "&".

The bold type is what I need to concentrate on. There is some info I'm
missing I can tell in dealing with this. Most of the pointers I've dealt
with is passing pointers to function parameters. I know you can also in
dealing with pointers to structs create an array of pointers to several
instances of a struct type. But I will catch onto that.

Thanks again for your help.

B
 
Ad

Advertisements

C

Charles Richmond

Xavier Roche said:
Le 18/05/2014 06:58, Bill Cunningham a écrit :
char p[3] = { "one", "two", "three" };

char p[3] is an array of char, and may only contain 3 "char", including
the terminating \0 (such as the string "hi")

did you mean char* p[3] ?

or, better,
const char* p[3] = { ... };

(string literals are constant, because even if their type is char[],
modifying it is "undefined" and segfaults on modern systems)

you are probably confusing "char" and "string". char is a (generally
8-bit) character.

You could also declare a two-dimensional array of character:

char p[][10] = {"one","two","three"};

The second dimension has to be long enough to hold the longest string plus
NULL terminator. Then you also can write:

pa[0] = p[2];
pa[1] = p[1];
pa[2] = p[0];

You do *not* need the "&" because p[1] etc. are already pointers to
character.

In *your* printf statement:

printf("%s %s %s\n", *(pa[0]), *(pa[1]), *(pa[2]));

You do *not* need the "*" in front of the pointer variables. The "%s"
expects a "pointer to character" parameter to print. Since "pa[0]" was
assigned the value of "p[2]", they both have the same "pointer to character"
value. It makes sense that you print them both without using a "*" in front
to dereference.
 
Ad

Advertisements

K

Keith Thompson

Charles Richmond said:
The second dimension has to be long enough to hold the longest string plus
NULL terminator. Then you also can write:
[...]

null or '\0' terminator, not NULL terminator. NULL is (a macro that
expands to) a null *pointer* constant, not a character constant.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

strange warning 192
C Programming functions 2
pointer addresses 15
C pipe 1
puzzling error 49
C exercise 1
struct problems 71
error in code 48

Top