Hello,
I am trying to append a space ' ', onto the end of a char array.
In my main function I must pass a string to another function.
eg) passingFunction("sentence...");
Appending a space must not be done in the main function.
However I am encountering a bus error problem.
Here is code that generates the error:
Code:
#include <stdio.h>
#include <string.h>
void printString(char *);
void passingFunction(char *);
int main()
{
passingFunction("I hate wonder bread.");
}
void passingFunction(char line[])
{
int length = strlen(line);
line[length] = ' ';
line[length+1] = '\0';
printString(line);
}
void printString(char line[])
{
int counter = 0;
char ch = line[counter];
while (ch != '\0')
{
printf("%c|", ch);
counter++;
ch = line[counter];
}
printf("\n");
}
I noticed that I do not encounter this problem if I did this:
Code:
#include <stdio.h>
#include <string.h>
void printString(char *);
void passingFunction(char *);
int main()
{
char line[] = "I hate wonder brea.");
passingFunction(line);
printString(line)
}
void passingFunction(char line[])
{
int length = strlen(line);
line[length] = ' ';
line[length+1] = '\0';
printString(line);
}
void printString(char line[])
{
int counter = 0;
char ch = line[counter];
while (ch != '\0')
{
printf("%c|", ch);
counter++;
ch = line[counter];
}
printf("\n");
}
What is causing this bus error and what would be a possible solution without declaring the array like I did in the second method I have?