0

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 chars 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

4 Answers 4

1

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]);
   }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I used your first solution. I got errors though about I couldn't declare j in the for loop and nullptr was unknown, so I declared j in the top of the function, and added '\0' as the last element in stationer[]
Oops, sorry, that was C not C++. I hope my edited code is better.
@Marius I wouldn't recommend to a C begineer to compute the size of an array using sizeof (as they would wonder why it doesn't work in a function, because of the promotion to pointer).
@Diti: on the other hand, maybe it's about time they learned why. :)
1

Change

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

and use

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

Comments

1

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.

Comments

0

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

1 Comment

Even better: strncat !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.