Segmentation fault on fgets [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
When I run the following code, it prints '>', waits for the input, and then segfaults. Does anybody know why?
int main(int argc, char **argv){
char input[MAX_INPUT_LINE];
while(1==1){
if (isatty(0)){
printf(">");
}
fgets(input, MAX_INPUT_LINE, stdin);
int len1=sizeof(input);
for (int i=0; i<len1; i++){
printf("%s", input[i]);
}
}

int len1=sizeof(input);
for (int i=0; i<len1; i++){
printf("%s", input[i]); // <-- %s would expect char * not char
}
Use %c to print a character not %s. %s would expect a nul terminated char * and you pass char, this causes undefined behaviour.
sizeof would return size of the array not length of string . So use strlen to get length of string.

Related

Why I got segmentation fault when using fgets in C? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I run the program with this command:
./word_search oi < text.txt
and got segmentation fault when running it.
This program is aiming to find where the word (giving as command line arg) exists in a file and print out those lines.
#include <stdio.h>
#include "substring.c"
int main(int argc, char ** argv) {
if(argc == 2) {
char *str;
while(fgets(str, 100, stdin)) {
if(substring(str, argv[1]) != -1) {
printf("Found: %s", str);
}
}
}
return 0;
}
If I change char *str into char str[100] then it works pretty good. Could anyone please tell me why?
The contents in substring.c:
#include <stdio.h>
#include <string.h>
int substring(const char *line, const char *substr) {
int i, j;
int result;
for(i = 0; i <= strlen(line)-strlen(substr); i++) {
result = 0;
if(line[i] == substr[0]) {
int c = i;
for(j = 0; j < strlen(substr); j++) {
if (line[c] != substr[j]) {
result = -1;
}
c++;
}
if(result != -1)
return i;
}
}
return -1;
}
The contents in test.txt are just several lines of meaningless characters.
char *str is an unitialized pointer, it cannot hold the string you are trying to copy into it, either allocate memory to it:
#include <stdlib.h>
#define SIZE 100
char *str = malloc(SIZE); //char has the size of 1 across platforms
Or simply declare it with the size you need:
char str[SIZE];
Pass the size of str to fgets
while(fgets(str, SIZE, stdin))
Of fgets:
Your container will be null terminated, it can only hold a string of SIZE - 1 characters.
All characters above SIZE - 1, including '\n' will remain unread and therefore in the buffer, you might need to clear it.
I suggest you take some time to learn basic C. Especially read about pointers, they are bit hard to get right at first.
In your example, str is a pointer to an undefined memory location.

I have an issue printing the length of a string from the command line without using strlen? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Hello there basically what my program is meant to do is when the user enters a string from the command line it will print the word and the size.
E.g
./commandline hello world
Output:
hello
world
2
What I'm trying to do is add a method that will print its length without using strlen so output should be length 10 for the example above.
This is my code bare in mind i am new to c.
int main(int args, char *argv[]){
for(int i =1; i <args; i++){
printf("%s\n", argv[i]);
size_t(argv[i]);
}
printf("%d\n", args -1);
}
size_t string_length( char *argv[]){
int length = 0;
while(argv[length]!='\0')
{
length++;
printf("%i\n", length);
}
return 0;
}
My program does not print length only prints the string entered and the size.
for(int i =1; i <args; i++){
printf("%s\n", argv[i]);
size_t(argv[i]);
}
printf("%d\n", args -1);
Here you're not calling your function anywhere. Your program just prints the arguments and the number of them. size_t(argv[i]); merely casts argv[i] to a type called size_t. Certainly, that's not what you want. Replace it with string_length(argv[i]);. Note that you'd better change the type of the first argument of this function.
What's more, you should return length in your string_length function.
size_t string_length( char *arg){
size_t length = 0;
while(arg[length])
{
length++;
}
return length;
}
try this
size_t string_length( char *argv[]);
int main(int args, char *argv[]){
for(int i =1; i <args; i++){
printf("%s\n", argv[i]);
string_length(argv[i]);
}
printf("%d\n", args -1);
}
size_t string_length( char *argv[]){
int length = 0;
while(argv[length]!='\0')
{
length++;
}
printf("%i\n", length);
return length;
}

Using a replace function in C programming [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
How do I write a replace function which takes a pointer to a string as a parameter and replaces all spaces in that string with minus signs?
example :
input = "I love pies", output = "I-love-pies"
This is what you need:
#include <stdio.h>
int change_string(char *input) {
for (char *p = input; *p; p++) {
if (*p == ' ') *p = '-';
}
return 1;
}
int main() {
char input[] = "I love pies";
printf("%s\n", input);
change_string(input);
printf("%s\n", input);
return 0;
}

What is the name of the buffer that can be overflowed buf, msg, len or out [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am new to this and I can't seem to understand can you please help me identify the buffer that can be overflowed and why?
#include <stdio.h>
#include <string.h>
#define S 100
#define N 1000
int main(int argc, char *argv[]) {
char out[S];
char buf[N];
char msg[] = "Welcome to the argument echoing program\n";
int len = 0;
buf[0] = '\0';
printf(msg);
while (argc) {
sprintf(out, "argument %d is %s\n", argc-1, argv[argc-1]);
argc--;``
strncat(buf,out,sizeof(buf)-len-1);
len = strlen(buf);
}
printf("%s",buf);
return 0;
}
The problem is here:
sprintf(out, "argument %d is %s\n", argc - 1, argv[argc - 1]);
When you face a problem like this one, it's always a good idea to go through your code commenting lines until the program stops crashing. Then you will know where the bug is :D

Passing strings as pointer to a function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
It's a simple function for many but as a beginner, I've yet to overcome the pointer ghost specially when it comes to strings. I understand some of the examples of strcmp, strcpy, strlen as well as how the characters are assigned in the memory with a NULL terminator. I think I also get the point how a pointer variable in memory points to the address of a int var or char etc. and you assign them by dereferencing them to var/char, but whenever I try to write a code, the pointer ghost comes back to bite me.
So, here I'm trying to run this and doesn't work. I would appreciate if you could clarify this for me...
//GETNAME function should return a string that is not NULL and less than 20 characters
char getname (char *s1)
{
int i, n;
char s2[i];
printf ("Enter your name:" );
scanf ("%s", "s2");
if (s2 == NULL)
return 0;
else if(n<20)
for ( i=0, n =strlen (s2 + 1); i<n; i++)
*(s1+i) = s2[i]; //copy characters from s2 and point to chars of s1
return *s1;
}
int main (int argc, char *argv[])
{
char name[20];
char urname;
urname = getname(name);
printf (" Your name is : %s\n", urname);
getch();
return NULL;
}
Here are several errors; there may be more:
Uninitialized variable:
int i, n;
char s2[i];
i is not initialized here, yet you use it as if it was. What value should i have? Like this it is undefined behaviour.
Incorrect argument to scanf:
scanf ("%s", "s2");
The second parameter should be a pointer to the memory that you want the input to be written to, not a constant string. It should be:
scanf ("%s", s2);
Incorrect argument to strlen:
for ( i=0, n =strlen (s2 + 1); i<n; i++)
You want to add 1 to the string length not to the string itself, so it should be
for ( i=0, n = strlen(s2) + 1; i<n; i++)
General issues with getname, including return type:
char getname (char *s1)
Why is this function so complex? You could directly scanf into the parameter s1. You don't need s2 for anything. Also the return type is wrong. You return a pointer, not a single char. It should be:
char* getname(char *s1)
Not handling return value from getname properly:
char urname;
urname = getname(name);
getname returns a pointer to char, not a single char. It should be:
char* urname;
urname = getname(name);
As the previous post says that i is not initialised.
Also the line
scanf("%s", "s2");
Should be
scanf("%s", s2);
The lines
if (s2 == NULL)
return 0;
else if(n<20)
Is incorrect as s2 will not be NULL and n is not initialised
... That is for starters
I recommend you get a book and read it

Resources