Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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
wHow can i make it type out the entire name?
Thanks,
Brds85
brds85@gmail.com

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]);
}

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 */
}

1. Your array declaration should be *arrayname[5] since there are 5 elements
2. Use %s for strings in printfchar *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]);

![]() |
![]() |
![]() |

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