2

I tried many things but I just cant find any solution to the problem. I am trying to print the contents of an array one by one but it crashes. How can I make each index corresponding letter print out after each iteration?

main() {
   int i;
   char myArray[10];

   for(i = 0; i < 5; i++) {
    myArray[i] = "a";
    printf("%s\n", myArray[i]);
   }

}

I also tried:

printf("%c\n", myArray[i])

and:

printf("%d\n", (int)myArray[i]) gives me the numbers 36.
10
  • 1
    You're using the %s (string) format specifier to print a character - tryn printf("%c\n", myArray[i]); Commented Jan 22, 2016 at 19:56
  • 3
    Also, you're assigning a string to a single char using "" double quotes, try single quotes for a sngle character - myArray[i] = 'a'; Commented Jan 22, 2016 at 19:57
  • 1
    What is the arr[i]? I don't see an arr declared (or initialized) anywhere. Commented Jan 22, 2016 at 19:58
  • 1
    You never initialized your myArray. arr is undefined. Commented Jan 22, 2016 at 19:59
  • 2
    Also, did you know you are only iterating through 5, but declared for 10? Commented Jan 22, 2016 at 20:00

3 Answers 3

2
  1. Your main function doesn't have a return value. You need to use either int or void.

  2. arr[i] = "a"; assigns a null-terminated string to arr[i], you want to assign a char.

  3. What is arr? The name of your array is myArray!

  4. printf("%s\n", myArray[i]); will interpret myArray[i] as a string, but it is a char.

This is the corrected program:

int main() 
{
    int i;
    char myArray[10];

    for (i = 0; i < 5; i++) 
    {
        myArray[i] = 'a';
        printf("%c\n", myArray[i]);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

It works when I do myArray[i] = 'a'; printf("%c\n", myArray[i]);
Ok, my compiler also allows me to leave out the return value, however you really should add the return value to main, otherwise it's very ugly
Yes, this as well. But also int main() { [...] } or void main() { [...] }
2
  1. arr is array with elements of type char - you should assign 'a' to each element instead of "a" (the latter gives you some address in memory - namely address where "a" is stored).

  2. %s is used to print null terminated string. Use %c instead due to type of elements in your array.

PS. Better use main like this:

int main(void)
{
  ...
  return 0;
}

1 Comment

you can remove the arr part. I changed the name and forgot to edit that one too. Corrected already
1

For the sake of completion, I felt obligated to point out that even return 0; isn't quite official, as I'll demonstrate below.

Also, did you know you are only iterating through 5, but declared for 10?

-e0k

@e0k thanks for making me notice. Ya, I need some sleep hehe

-Asperger

In order for you to remember to iterate through the same amount of indices as you declared in your array, you should #define your array length like this:

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

#define ARRAY_SIZE 10

int main(void) // we're not using argc and argv
{
    size_t i; // use size_t since i is used as an array index
    char myArray[ARRAY_SIZE];

    for (i = 0; i < ARRAY_SIZE; i++) 
    {
        myArray[i] = 'a';
        printf("%c\n", myArray[i]);
    }

    return EXIT_SUCCESS; // defined in stdlib.h
}

Comments

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.