0

So, I uncovered a "discrepancy"--or, more likely, a gap in my understanding of arrays in C. Below is a trivial program to reverse strings. The discrepancy is noted within the code's comments.

#include <stdio.h>
#include <string.h>

void reverser(char to_bb[]){
    printf("%s\n", to_bb[some_valid_number]); /* This results in a bus error */
    int counter = strlen(to_bb);              /* ^ Assume actual integer */
    char reversed[counter];         
    int counter2 = 0;
    for(--counter; counter >= 0; counter--){     
        reversed[counter2] = to_bb[counter];  /* This does not */
        counter2++;
    }
    reversed[counter2] = '\0';
    printf("The reversed: %s\n", reversed);
}

int main(){
    char to_be_reversed[20];
    puts("Enter the string to be reversed: ");
    scanf("%19s", to_be_reversed);
    reverser(to_be_reversed);
    return 0;
}

Why does printf(); result in a bus error, while calling the elements to swap them into a different array does not? Aren't they calling the same thing?

4
  • 3
    Passing in an incompatible argument to printf isn't a good thing to do. Commented Jan 7, 2013 at 20:21
  • @H2CO3 According to my output it will be! Commented Jan 7, 2013 at 20:33
  • 1
    @d0rmLife Oh, well, it will. You haven't made the typical beginner mistake, sorry for the prejudice :P Commented Jan 7, 2013 at 20:34
  • @H2CO3 Don't worry, I'm still a beginner... for now! >:) Commented Jan 7, 2013 at 20:41

1 Answer 1

7
printf("%s\n", to_bb[some_valid_number]);

s conversion specifier requires a pointer to char but to_bb[some_valid_number] is of type char.

To get a pointer to to_bb[some_valid_number] object you can use the & operator:

printf("%s\n", &to_bb[some_valid_number]);

If instead you want to print the to_bb[some_valid_number] character, use the c conversion specifier:

printf("%c\n", to_bb[some_valid_number]);
Sign up to request clarification or add additional context in comments.

6 Comments

Do I have to create a pointer to to_bb[some_valid_number] or is there a different way to specify this in printf();?
@d0rmLife Please RTFM (Read the Formatted I/O Manual). %c is what you're looking for (and a good tutorial on arrays and pointers).
@H2CO3 Thanks for the F$%^ing manual... it can be comically difficult to discover such obvious resources as a beginner! Thanks again
&to_bb[some_valid_number] is same as to_bb + some_valid_number
@d0rmLife chars are just variables, there is no such thing as strings, just arrays or chars. So when you need one char, you can use it's value directly or it's address to get the value. But for strings, you have to use the address of the first one, so you can jump to the next one and next and next untill you reach \0. (I'm using words pointer and address interchangeably here). (You can also think of arrays as pointers, for a beginner it's a justifiable simplification)
|

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.