Home > database >  How to make an array of character in C
How to make an array of character in C

Time:11-23

I am trying to make an array of character. I get this code and get error

#include<stdio.h>
#include<stdlib.h>
#include"boolean.h"

const char *skillNames[8];
skillNames[0] = "Darth Vader";
skillNames[1] = "Han Solo";
skillNames[2] = "Master Yoda";
skillNames[3] = "Luke Skywalker";
skillNames[4] = "Obi Wan Kenobi";
skillNames[5] = "Chewbacca";
skillNames[6] = "Emperor Palpatine";
skillNames[7] = "Princess Leia";

CodePudding user response:

#include<stdio.h>
#include<stdlib.h>
#include"boolean.h"

int main() {
    const char *skillNames[8];
    skillNames[0] = "Darth Vader";
    skillNames[1] = "Han Solo";
    skillNames[2] = "Master Yoda";
    skillNames[3] = "Luke Skywalker";
    skillNames[4] = "Obi Wan Kenobi";
    skillNames[5] = "Chewbacca";
    skillNames[6] = "Emperor Palpatine";
    skillNames[7] = "Princess Leia";
}

Put it inside a main function.

CodePudding user response:

You can use the initialization syntax. You don't even need to mention the size of the array:

const char *skillNames[] = {
    "Darth Vader",    "Han Solo",  "Master Yoda",       "Luke Skywalker",
    "Obi Wan Kenobi", "Chewbacca", "Emperor Palpatine", "Princess Leia",
};

CodePudding user response:

Add a main() function and replace the #include"boolean.h" with #include<stdbool.h>. See if that fixes the error or not.

The full code can be like this:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h> 
int main()
{
    const char *skillNames[8];
    skillNames[0] = "Darth Vader";
    skillNames[1] = "Han Solo";
    skillNames[2] = "Master Yoda";
    skillNames[3] = "Luke Skywalker";
    skillNames[4] = "Obi Wan Kenobi";
    skillNames[5] = "Chewbacca";
    skillNames[6] = "Emperor Palpatine";
    skillNames[7] = "Princess Leia";
}
  •  Tags:  
  • c
  • Related