Computing.Net > Forums > Programming > Char * Array[strings]

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Char * Array[strings]

Reply to Message Icon

Name: brds
Date: November 16, 2005 at 10:37:47 Pacific
OS: windows xp
CPU/Ram: 512mg
Comment:

I was wondering how to have an array using strings. I've tried doing the following:

char *arrayName[4];
*arrayName[0] = "purple";
*arrayname[1] = "red";
*arrayname[2] = "blue";
*arrayname[3] = "orange";
*arrayname[4] = "white";

However, when i try to print them out, by doing the following:

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

all i get is the first letter of the input color that i typed. For example if i make a loop to print out all the colors in the array using the printf method above, i would get:

p
r
b
o
w

How can i make it type out the entire name?

Thanks,
Brds85
brds85@gmail.com




Sponsored Link
Ads by Google

Response Number 1
Name: dtech10
Date: November 16, 2005 at 12:44:36 Pacific
Reply:

Hi

#include <stdio.h>

void main() {
rem [6]=String Length
char *Name[4][6];
*Name[0] = "purple";
*Name[1] = "red";
*Name[2] = "blue";
*Name[3] = "orange";
*Name[4] = "white";
rem %s, %c = character
printf("%s\n", *Name[4]);
}


0

Response Number 2
Name: nails
Date: November 18, 2005 at 11:09:43 Pacific
Reply:

Hi:

There's more than one way of initializing an array:

#include <stdio.h>

int main()
{

char *Name[] =
{
"purple",
"red",
"blue",
"orange",
"white"
};

printf("%s\n", Name[0]); /* purple */
printf("%s\n", Name[4]); /* white */
}


0

Response Number 3
Name: egkenny
Date: November 18, 2005 at 11:32:29 Pacific
Reply:

1. Your array declaration should be *arrayname[5] since there are 5 elements
2. Use %s for strings in printf

char *arrayname[5];
arrayname[0] = "purple";
arrayname[1] = "red";
arrayname[2] = "blue";
arrayname[3] = "orange";
arrayname[4] = "white";

for(int i=0;i<5;i++)
printf("%s\n", arrayname[i]);


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Char * Array[strings]

Converting a String to a char array www.computing.net/answers/programming/converting-a-string-to-a-char-array/3190.html

C++ 2 dimensional char array www.computing.net/answers/programming/c-2-dimensional-char-array/10085.html

v.c++changing cstring to char array www.computing.net/answers/programming/vcchanging-cstring-to-char-array/14282.html