d0rmLife
d0rmLife

Reputation: 4250

C: How to put an arbitrary array in available memory? (basic)

So, I'm trying to make a trivial program to reverse strings. I am very wet behind the ears when it comes to C, especially in understanding memory. Here's my code:

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

void reverser(char to_bb[]){
    int counter = strlen(to_bb);      /* size of entry */
    char reversed[counter];         /* creating blank array of same size */
    int counter2 = 0;
    for(counter - 1; counter >= 0; counter--){     /* loop to swap chars */
            reversed[counter2] = to_bb[counter];
            counter2++;
    }
    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;
}

The problem is, I keep getting a bus error. I used sizeof(to_bb) and learned that it was 4 bits, indicating to_bb[] is merely a pointer within my void function. I have tried many ways to extract the string so I can manipulate it (i.e. call its elements), but I have not found success. I tried strcpy() and more long-winded declarations using *.

Thanks for any advice. Memory is proving challenging to understand!

Upvotes: 0

Views: 112

Answers (3)

Mike
Mike

Reputation: 49393

You need to learn to compile with warnings:

gcc -Wall yourfile.c

In function 'reverser':
11: 5: warning: statement with no effect [-Wunused-value]

The first part of a for statement is assignment: counter - 1 does no assignment. Thus it does nothing and hence you get a warning telling you that you're not doing anything useful.

Another really useful operator you might want to learn about is the , opperator. It allows you to accomplish looping like this with less lines in the body of your loop.

for(counter -= 1; counter >= 0; counter--, counter2++)     /* loop to swap chars */

Finally you're not accounting for the NULL terminator. The function strlen() gives you the number of characters in the string not including the NULL terminator.

So you should account for that yourself then after the loop append it, something like:

char reversed[counter+1];
int counter2 = 0;
for(counter - 1; counter >= 0; counter--, counter2++)     /* loop to swap chars */
        reversed[counter2] = to_bb[counter];
reversed[counter2] = '\0';
printf("The reversed: %s\n", reversed);

Upvotes: 1

Eric Postpischil
Eric Postpischil

Reputation: 222660

In the for, change counter - 1 to counter = counter-1 or, equivalently, --counter.

Add one more character to the size of reversed and set it to '\0', so that it is null terminated. Alternatively, you can restrict the number of characters printed by using:

printf("The reversed: %.*s\n", counter2, reversed);

Upvotes: 1

Bart Friederichs
Bart Friederichs

Reputation: 33511

You start with putting the \0 character in your array, creating an empty string.

Quick fix, change

for(counter - 1; counter >= 0; counter--){ 

into

for(--counter; counter >= 0; counter--){ 

Upvotes: 1

Related Questions