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?
printfisn't a good thing to do.