using getchar to receive input from file and insert into char pointer giving segmentation fault - c

There is a file called prog1.txt that is being read into my program. To execute, I use the name of the file and a '<' symbol to read in the file. (./a.out < prog1.txt). When i read it in however, i immediately receive a segmentation fault. Here is what I have written that is giving this issue:
char *strPtr;
while(((*strPtr = getchar()) != EOF) && (*strPtr != '\n')) {
strPtr++;
}
I have researched other questions, but I can't find a problem whose solution is usable for this. What is causing the segmentation fault??

You have a pointer char* strPtr, but it might not point to anything that you can use. You'll need to allocate some memory. char* strPtr = malloc(numChars); You should also free it after you're finished with the allocated memory: free(strPtr);
You'll need to set a maximum number of characters you can read in. In this case I use numChars.

Creating a pointer does not create something for it to point at. When using a pointer (or array syntax) it is the programmer's responsibility to ensure things are set up correctly.
getchar() returns int, and EOF is a value that cannot be represented using a char. Comparing any value of type char with EOF will therefore always fail.
You need to deal with both these concerns. You are not.
For example;
char *strPtr = malloc(10);
int length = 0;
if (strPtr != NULL)
{
int achar;
while(length < 10 && (achar = getchar()) != EOF && achar != '\n')
{
strPtr[length] = (char)achar;
++length;
}
}
/* do whatever is needed with strPtr and length*/
free(strPtr);
The check that strPtr is not NULL is to ensure the malloc() call succeeded before trying to write to the allocated array.
length is being used to ensure the code does not write to strPtr past the allocated length.
achar is used to check for EOF before converting the value to a char (not after, as in your code).

Use an array
char strPtr[10];
int temp;
int i = 0;
while( i < 10 && ((temp = getchar()) != EOF) && (temp != '\n')) {
strPtr[i] = temp;
i++;
}

Related

Pico W Read Serial Input

I'm trying to read the input sent through the serial monitor but scanf does nothing and i can't figure out how to get getchar to do the same thing.
Here's a piece of code that I've tried to use that does not work on the pico. It does work on my pc.
char *readInput() {
int i = 0;
char ch;
char *str = malloc(sizeof(char) * 1024);
while((ch = getchar()) != '\n' && ch != EOF) {
if (i < 1024) {
str[i++] = ch;
printf("%c\n", ch); // For debugging. Returns the correct character.
}
}
printf("%s\n", str); // For debugging. Returns nothing.
str[i] = '\0';
return str;
}
It looks like it just tries to read until it gets to the 1024th character.
Also I've read somewhere that scanf doesn't work on the Pico but some people say it works so i don't even know anymore.
malloc()'s result is discarded:
char *str = malloc(sizeof(char) * 1024);
malloc() returns a NULL pointer to indicate failure, which it can and does. Its return value should always be checked. If it returns NULL, subsequent operations would be dereferencing and writing to a NULL pointer.
Aside: sizeof (char) is defined to be 1, so you can leave that out.
char *str = malloc(1024);
/* Add */
if (!str) {
/* malloc() failed to allocate memory.
* Handle error here.
*/
}
Off-by-one error:
if (i < 1024)
doesn't leave room for the null-byte.
getchar() returns an int:
char ch;
while((ch = getchar()) != '\n' && ch != EOF)
If the integer value returned by getchar() is stored into a
variable of type char and then compared against the integer
constant EOF, the comparison may never succeed, because sign-
extension of a variable of type char on widening to integer is
implementation-defined.
Undefined behaviour:
printf("%s\n", str); // For debugging. Returns nothing.
The %s format specifier expects a string. str is not a string. The call to printf() would invoke undefined behaviour.
Move
str[i] = '\0';
before the call to printf().
Writing to out of bounds memory:
str[i] = '\0';
The value of i is 1024 when the while loop exits. You only allocated memory for 1023 characters.
Minor:
char *readInput()
indicates that readInput takes an unspecified number and type of arguments.
char *readInput (void)
doesn't.

Return a string made with a line read from input

i am trying to code a C function which returns a line read from the input as a char* . I am on Windows and i test my program in the command line by giving files as input and output of my program like this:
cl program.c
program < test_in.txt > test_out.txt
This is my (not working) function:
char* getLine(void)
{
char* result = "";
int i, c;
i = 1;
while((c = getchar()) != EOF)
{
*result++ = c;
i++;
if(c == '\n')
return result - i;
}
return result - i;
}
I was expecting it to work because i previously wrote:
char* getString(char* string)
{
//char* result = string; // the following code achieve this.
char* result = "";
int i;
for(i = 1; *result++ = *string++; i++);
return result - i;
}
And these lines of code have a correct behaviour.
Even if every answers will be appreciated, i would be really thankfull
if any of you could explain me why my getString() function works while my getLine() function doesn't.
Your function does not allocate enough space for the string being read. The variable char* result = "" defines a char pointer to a string literal ("", empty string), and you store some arbitrary number of characters into the location pointed to by result.
char* getLine(void)
{
char* result = ""; //you need space to store input
int i, c;
i = 1;
while((c = getchar()) != EOF)
{
*result++ = c; //you should check space
i++;
if(c == '\n')
return result - i; //you should null-terminate
}
return result - i; //you should null-terminate
}
You need to allocate space for your string, which is challenging because you don't know how much space you are going to need a priori. So you need to decide whether to limit how much you read (ala fgets), or dynamically reallocate space as you read more. Also, how to you indicate that you have finished input (reached EOF)?
The following alternative assumes dynamic reallocation is your chosen strategy.
char* getLine(void)
{
int ch; int size=100; size_t pos=0;
char* result = malloc(size*sizeof(char*));
while( (ch=getchar()) != EOF )
{
*result++ = ch;
if( ++pos >= size ) {
realloc(result,size+=100);
//or,realloc(result,size*=2);
if(!result) exit(1); //realloc failed
}
if( c=='\n' ) break;
}
*result = '\0'; //null-terminate
return result - pos;
}
When you are done with the string returned from the above function, please remember to free() the allocated space.
This alternative assumes you provide a buffer to store the string (and specifies the size of the buffer).
char* getLine(char* buffer, size_t size)
{
int ch;
char* result = buffer;
size_t pos=0;
while( (ch=getchar()) != EOF )
{
*result++ = ch;
if( ++pos >= size ) break; //full
if( c=='\n' ) break;
}
*result = '\0'; //null-terminate
return buffer;
}
Both avoid the subtle interaction between detecting EOF, and having enough space to store a character read. The solution is to buffer a character if you read and there is not enough room, and then inject that on a subsequent read. You will also need to null-ter
Both functions have undefined behaviour since you are modifying string literals. It just seems to work in one case. Basically, result needs to point to memory that can be legally accessed, which is not the case in either of the snippets.
On the same subject, you might find this useful: What Every C Programmer Should Know About Undefined Behavior.
Think of it this way.
When you say
char* result = "";
you are setting up a pointer 'result' to point to a 1-byte null terminated string (just the null). Since it is a local variable it will be allocated on the stack.
Then when you say
*result++ = c;
you are storing that value 'c' in to that address + 1.
So, where are you putting it?
Well, most stacks are to-down; so they grow toward lower addresses; so, you are probably writing over what is already on the stack (the return address for whatever called this, all the registers it needs restore and all sorts of important stuff).
That is why you have to be very careful with pointers.
When you expect to return a string from a function, you have two options (1) provide a string to the function with adequate space to hold the string (including the null-terminating character), or (2) dynamically allocate memory for the string within the function and return a pointer. Within your function you must also have a way to insure your are not writing beyond the end of the space available and you are leaving room for the null-terminating character. That requires passing a maximum size if you are providing the array to the function, and keeping count of the characters read.
Putting that together, you could do something similar to:
#include <stdio.h>
#define MAXC 256
char* getLine (char *s, int max)
{
int i = 0, c = 0;
char *p = s;
while (i + 1 < max && (c = getchar()) != '\n' && c != EOF) {
*p++ = c;
i++;
}
*p = 0;
return s;
}
int main (void) {
char buf[MAXC] = {0};
printf ("\ninput : ");
getLine (buf, MAXC);
printf ("output: %s\n\n", buf);
return 0;
}
Example/Output
$ ./bin/getLine
input : A quick brown fox jumps over the lazy dog.
output: A quick brown fox jumps over the lazy dog.

Trouble solving uninitialised value error given by Valgrind

I'm currently writing a test program that parses input from a stream. I won't go into too much detail about this program but I am currently trying to parse alphanumeric characters and then assign them to a temp string, temp[100]. After all valid characters are assigned to temp, I allocate memory and strncpy to the allocated string variable.
Valgrind complains about my two usages of strlen and my single use of strncpy. Why is this? It complains about an uninitialised value but I made it clear that it won't do any allocation unless there are characters inside temp. Any suggestions?
char *name(char a)
{
int x;
char c;
char *returnName = 0;
char temp[100];
int i = 0;
/* Ensures no character is skipped */
temp[i] = a;
i++;
/* Fill temp one character at a time */
while((x = getchar()) != EOF)
{
c = (char)x;
/* Valid characters are assigned */
if((isalnum(c)) || c == '_')
{
temp[i] = c;
i++;
}
/* As soon as invalid character appears, exit loop */
else
break;
}
/* Make sure temp is not NULL before mallocing */
if(temp[0] != '\0') /* Thank you Alter Mann for this fix */
{
printf("Before malloc\n");
returnName = malloc(sizeof(char)*strlen(temp)+1);
printf("After malloc before strncpy\n");
strncpy(returnName, temp, strlen(temp)+1);
printf("After strncpy before return\n");
return returnName;
}
/* If nothing is assigned, return NULL */
return NULL;
}
You never null-terminated your string in temp, so both strlen() and strcpy() are reading past the initialized values in your array, hence the uninitialized value errors Valgrind is giving you.
Change:
char temp[100];
to:
char temp[100] = {0};
and you should be good.
Here:
if(temp != NULL)
You need to check
if(temp[0] != '\0')
temp is an array, not a pointer.
And (as pointed out by Paul Griffiths), NUL-terminate your string after the while loop:
temp[i] = '\0';

Dynamically created C string

I'm trying to get an expression from the user and put it in a dynamically created string. Here's the code:
char *get_exp() {
char *exp, *tmp = NULL;
size_t size = 0;
char c;
scanf("%c", &c);
while (c != EOF && c != '\n') {
tmp = realloc(exp, ++size * sizeof char);
if (tmp == NULL)
return NULL;
exp = tmp;
exp[size-1] = c;
scanf("%c", &c);
}
tmp = realloc(exp, size+1 * sizeof char);
size++;
exp = tmp;
exp[size] = '\0';
return exp;
}
However, the first character read is a newline char every time for some reason, so the while loop exits. I'm using XCode, may that be the cause of the problem?
No, XCode is not part of your problem (it is a poor workman who blames his tools).
You've not initialized exp, which is going to cause problems.
Your code to detect EOF is completely broken; you must test the return value of scanf() to detect EOF. You'd do better using getchar() with int c:
int c;
while ((c = getchar()) != EOF && c != '\n')
{
...
}
If you feel you must use scanf(), then you need to test each call to scanf():
char c;
while (scanf("%c", &c) == 1 && c != EOF)
{
...
}
You do check the result of realloc() in the loop; that's good. You don't check the result of realloc() after the loop (and you aren't shrinking your allocation); please check every time.
You should consider using a mechanism that allocates many bytes at a time, rather than one realloc() per character read; that is expensive.
Of course, if the goal is simply to read a line, then it would be simplest to use POSIX getline(), which handles all the allocation for you. Alternatively, you can use
fgets() to read the line. You might use a fixed buffer to collect the data, and then copy that to an appropriately sized dynamically allocated buffer. You would also allow for the possibility that the line is very long, so you'd check that you'd actually got the newline.
Here on Windows XP/cc, like Michael said, it works if exp is initialized to NULL.
Here's a fixed code, with comments explaining what is different from your code in the question:
char *get_exp()
{
// keep variables with narrowest scope possible
char *exp = NULL;
size_t size = 0;
// use a "forever" loop with break in the middle, to avoid code duplication
for(;;) {
// removed sizeof char, because that is defined to be 1 in C standard
char *tmp = realloc(exp, ++size);
if (tmp == NULL) {
// in your code, you did not free already reserved memory here
free(exp); // free(NULL) is allowed (does nothing)
return NULL;
}
exp = tmp;
// Using getchar instead of scanf to get EOF,
// type int required to have both all byte values, and EOF value.
// If you do use scanf, you should also check it's return value (read doc).
int ch = getchar();
if (ch == EOF) break; // eof (or error, use feof(stdin)/ferror(stdin) to check)
if (ch == '\n') break; // end of line
exp[size - 1] = ch; // implicit cast to char
}
if (exp) {
// If we got here, for loop above did break after reallocing buffer,
// but before storing anything to the new byte.
// Your code put the terminating '\0' to 1 byte beyond end of allocation.
exp[size-1] = '\0';
}
// else exp = strdup(""); // uncomment if you want to return empty string for empty line
return exp;
}

Tokenizing user input in C (store in **arg)?

I'm attempting to write a simple shell like interface, that takes in a users input (by char) and stores it via a pointer to a pointer* (exactly how argv works). Here's my code:
char input[100];
char **argvInput;
char ch;
int charLoop = 0;
int wordCount = 0;
argvInput = malloc(25 * sizeof(char *));
while((ch = getc(stdin))) {
if ((ch == ' ' || ch == '\n') && charLoop != 0) {
input[charLoop] = '\0';
argvInput[wordCount] = malloc((charLoop + 1) * sizeof(char));
argvInput[wordCount] = input;
charLoop = 0;
wordCount++;
if (ch == '\n') {
break;
}
} else if (ch != ' ' && ch != '\n') {
input[charLoop] = ch;
charLoop++;
} else {
break;
}
}
If I loop through argvInput via:
int i = 0;
for (i = 0; i < wordCount; i++)
printf("Word %i: %s\n", i, argvInput[i]);
All of the values of argvInput[i] are whatever the last input assignment was. So if I type:
"happy days are coming soon", the output of the loop is:
Word 0: soon
Word 1: soon
Word 2: soon
Word 3: soon
Word 4: soon
I'm at a loss. Clearly each loop is overwriting the previous value, but I'm staring at the screen, unable to figure out why...
This line is your bane:
argvInput[wordCount] = input;
Doesn't matter that you allocate new space, if you're going to replace the pointer to it with another one (i.e. input).
Rather, use strncpy to extract parts of the input into argvInput[wordCount].
argvInput[wordCount] = input; is only making the pointer of argvInput[wordCount] point to the memory of input instead of copy the content of input into the new allocated memory. You should use memcpy or strcpy to correct your program.
After the pointer assignment the memory status looks like the image below. The memory allocated by malloc((charLoop + 1) * sizeof(char));, which are the grey ones in the graph, could not be accessed by your program anymore and this will lead to some memory leak issue. Please take care of that.
I suggest printing your argvInput pointers with %p, instead of %s, to identify this problem: printf("Word %i: %p\n", i, (void *) argvInput[i]);
What do you notice about the values it prints? How does this differ from the behaviour of argv? Try printing the pointers of argv: for (size_t x = 0; x < argc; x++) { printf("Word %zu: %p\n", x, (void *) argv[x]); }
Now that you've observed the problem, explaining it might become easier.
This code allocates memory, and stores a pointer to that memory in argvInput[wordCount]: argvInput[wordCount] = malloc((charLoop + 1) * sizeof(char)); (by the way, sizeof char is always 1 in C, so you're multiplying by 1 unnecessarily).
This code replaces that pointer to allocated memory with a pointer to input: argvInput[wordCount] = input; ... Hence, all of your items contain a pointer to the same array: input, and your allocated memory leaks because you lose reference to it. Clearly, this is the problematic line; It doesn't do what you initially thought it does.
It has been suggested that you replace your malloc call with a strdup call, and remove the problematic line. I don't like this suggestion, because strdup isn't in the C standard, and so it isn't required to exist.
strncpy will work, but it's unnecessarily complex. strcpy is guaranteed to work just as well because the destination array is allocated to be large enough to store the string. Hence, I recommend replacing the problematic line with strcpy(argvInput[wordCount], input);.
Another option that hasn't been explained in detail is strtok. It seems this is best left unexplored for now, because it would require too much modification to your code.
I have a bone to pick with this code: char ch; ch = getc(stdin); is wrong. getc returns an int for a reason: Any successful character read will be returned in the form of an unsigned char value, which can't possibly be negative. If getc encounters EOF or an error, it'll return a negative value. Once you assign the return value to ch, how do you differentiate between an error and a success?
Have you given any thought as to what happens if the first character is ' '? Currently, your code would break out of the loop. This seems like a bug, if your code is to mimic common argv parsing behaviours. Adapting this code to solve your problem might be a good idea:
for (int c = getc(stdin); c >= 0; c = getc(stdin)) {
if (c == '\n') {
/* Terminate your argv array and break out of the loop */
}
else if (c != ' ') {
/* Copy c into input */
}
else if (charLoop != 0) {
/* Allocate argvInput[wordCount] and copy input into it,
* reset charLoop and increment wordCount */
}
}

Resources