简体   繁体   中英

Printing String from array

I know how to program Java and now I'm trying to learn C programming.

I want to write out a String from an Array of char s that I have created, and write out every String in one line (I'm creating a console application).

So far I have got this;

int main()
{
    int i;
    int j;
    char aarhus[] = {'\x8F','r','h','u','s',' ','H','\0'};
    char esbjerg[] = {'E','s','b','j','e','r','g','\0'};
    char stationer[] = {*aarhus,*esbjerg};
        for (j=0;stationer[j] != '\0';j++) {
            printf("%c\n", stationer[j]);
        }
    return 0;
}

with the includes in the top of course. But when I launch the program, it only writes ÅEEsbjerg and not Århus Esbjerg (both on a single line for itself, as I want it to). If I add \\0 in the stationer[] , all I get is ÅE on the same line. I have been trying to fix this for hours but nothing so far. I hope you can help. Thanks in advance

You're probably looking for something like this:

int main()
{
   int j;
   char aarhus[] = {'\x8F', 'r', 'h', 'u', 's', ' ', 'H', '\0'};
   char esbjerg[] = {'E', 's', 'b', 'j', 'e', 'r', 'g', '\0'};
   char* stationer[] = {aarhus, esbjerg, 0};
   for(j = 0; stationer[j] != 0; j++) 
   {
      printf("%s", stationer[j]);
   }
}

or maybe this:

int main()
{
   int j;
   char aarhus[] = {'\x8F', 'r', 'h', 'u', 's', ' ', 'H', '\0'};
   char esbjerg[] = {'E', 's', 'b', 'j', 'e', 'r', 'g', '\0'};
   char* stationer[] = {aarhus, esbjerg};
   int count = sizeof(stationer) / sizeof(char*);
   for(j = 0; j < count; j++) 
   {
      printf("%s", stationer[j]);
   }
}

Change

 char *stationer[] = {aarhus,esbjerg};

and use

 printf("%s", stationer[j]);

Your code has many problems.

Arrays in C are not terminated. Your loop for (j=0;stationer[j] != '\\0';j++) { seems to assume stationer contains the character '\\0', yet your definition did not contain it.

Second, your program currently only prints two characters which are contained in stationer . The fact you see more characters of the esbjerg array is an example of undefined behaviour - you have run past the end of the array, therefore you are reading (and printing) garbage.

Furthermore, you have not included a newline '\\n' character in your printf format. printf will not do that automatically for you like println does.

So, to sum up:

int main()
{
    char aarhus[] = {'\x8F','r','h','u','s',' ','H','\0'};
    char esbjerg[] = {'E','s','b','j','e','r','g','\0'};

    /* An array of pointers to char. */
    char *stationer[] = {aarhus, esbjerg, NULL};

    int index = 0;
    /* Print each array of character as a string "%s": */
    for (index = 0 ; stationer[index] != NULL; ++index) {
            printf("%s", stationer[index]);
    }
    return 0;
}

Last but not least, printf will handle 'Å' correctly if your locale supports it.

This should work:

printf("%s", aarhus);
printf("%s", esbjerg);

Because you don't add a \\n they should be printed on the same line.

About your early termination I think it is the initialization of stationer that is in cause. You should try something like this (you want to double-check the sizes considering \\0 , \\n etc):

// Allocate a new string 
unsigned char * stationer = malloc(sizeof(aarhus)+sizeof(esbjerg) -1); // -1 for the extra \0
// Copy your 1st string
memcpy(stationer, aarhus, sizeof(aarhus));
// Append the second
memcpy(stationer + sizeof(aarhus), esbjerg, sizeof(esbjerg));
// print it
printf("%s", stationer);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM