I'm trying to implement a C function char* strShiftLeft(char* str, int n) that takes in a string (str) and an int n, which represents how many chars to shift str to the left. For example, if we call it as strShiftLeft("Thomas", 2), the function should return a char pointer pointing to a string "omasTh" as we append the first 2 (n) chars to the end of string and shift every char 2 slots to the left.
However, for some reason, when I compile my program, it compiles fine but when I run it on my mac, my terminal keeps showing me:
zsh: bus error ./a.out
Here's my code:
#include <stdio.h>
#include <stdlib.h>
char* strShiftLeft(char* str, int n)
{
// If the passed in string is NULL, return NULL
if (str == NULL)
{
printf("NULL!\n");
return NULL;
}
// Get the length of the string
int len = 0;
char* counter_ptr = str;
while (*counter_ptr != '\0')
{
len++;
counter_ptr++;
}
printf("len: %d\n", len);
// If the integer is longer than the string, return back NULL
if (n > len)
{
printf("n too big!\n");
return NULL;
}
// Create a temp char array to store 1st n chars to append to the end of string later
char temp_arr[n];
for (int i = 0; i < n; i++)
{
*(temp_arr + i) = *(str + i);
}
printf("temp_arr = %s\n", temp_arr);
printf("str is still: %s\n", str);
// So far so good
char* temp = str + n;
printf("len - n = %d\n", len - n);
// Update first (len - n) characters of the string (e.g. n = 2, "Thomas" -> "omasas")
for (int i = 0; i < (len - n); i++)
{
printf("*temp = %c\n", *temp);
printf("*(temp - n) = %c\n", *(temp - n));
*(temp - n) = *(temp); // THIS LINE SEEMS TO BE THE ONE CAUSING ERRORS
temp++;
}
temp = str + (len - n);
for (int i = 0; i < n; i++)
{
*(temp + i) = *(temp_arr+i); // THIS LINE ALSO SEEMS TO BE CAUSING ERRORS
}
return str;
}
int main()
{
char* str = strShiftLeft("Thomas", 2);
printf("%s\n", str); // Should print out "omasTh"
return 0;
}
I used a lot of printf()s to try to find out what went wrong. I noticed two particular lines of code seem to be causing some errors, namely: 1)
*(temp - n) = *(temp);
*(temp + i) = *(temp_arr+i);
What I'm trying to do with these 2 lines of code is that I want to update the values of certain elements in the string with values from other elements in the string.
Although my code compiles, when I run it, it shows "zsh: bus error ./a.out". I tried using some online compiler. When I ran my code, I got seg fault.
I don't know what mistakes I've made.
Would be much appreciated if someone can help me identify the mistakes I've made.
Note that, I'm not allowed to use dynamic memory allocation, bracket operator for dereferencing pointers, or any functions from the C library such as strlen(), strcpy() etc. Thanks!
I've used printf() to pinpoint which lines are causing errors, but I don't understand why they're causing errors.