i tried using this code for deleting a user given character from a user given string
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
The problem which causes the segfault is that you are trying to scanf into an uninitialized pointer to nowhereland.
To use scanf in the way you try, you need to provide a sufficiently large buffer for the data you read. The easiest way to do this is to change your code as follows:
Code:
// cut this line. Its just wrong: char *name;
// use this line instead:
char name[100];
This declaration makes the compiler get enough memory for 100 char. This memory can than be accessed by name.
To prevent further errors you also should change your scanf - format string as follows.
Code:
scanf("%99s %c",name,&c);
This will make the scanf function read up to 99 characters from stdin and append a terminating '\0' (100th char).
By the way, your code is broken in a few other ways too.
Edit:
You didn't understand what "the others" where trying to tell you here, did you?
Edit2:
dwhitney67 is absolutely correct about using fgets. Just listen to him ;-)
regards, Heraton
Last edited by Heraton; 06-04-2011 at 11:18 AM.
Reason: corrected format string
Where do you supposed scanf() is going to store the string that is entered? Undoubtedly you want this string stored at the memory location referenced by the variable 'name'. And what memory location is 'name' referencing? (hint: random location)
If you wish for the user to enter a word and character, do you not think that the format statement above will require a white-space between the %s and %c?
Also, why use a variable called 'name'? Do you not think it would be more appropriate to use a variable called 'word' or something similar? Use of single-letter variable names should be avoided; consider declaring variables that are descriptive of the data they store.
As for using scanf() to read in a string, it is not the wisest of choices. Consider using fgets() to read the entire input from the user, and then parse the inputted string for the word and character. Using your code as an example, here's the gist of it:
Code:
#include <stdio.h>
#include <stdlib.h>
char* delete_char_from_string(const char* str, const char ch);
char* reverse_string(const char* str);
int main()
{
char input[30];
char word[24];
char delete_ch;
printf("Enter word and a character to be deleted: ");
fgets(input, sizeof(input), stdin); /* use fgets() to read entire input */
/* extract word and character from input */
if (sscanf(input, "%s %c", word, &delete_ch) != 2)
{
fprintf(stderr, "Failed to parse input string; exiting!\n");
return -1;
}
char* new_str = delete_char_from_string(word, delete_ch);
char* rev_str = reverse_string(word);
if (rev_str)
{
printf("Reversed word: %s\n", rev_str);
free(rev_str);
}
if (new_str)
{
printf("Word without %c: %s\n", delete_ch, new_str);
free(new_str);
}
return 0;
}
/*
* Delete the given character from the string, without affecting the original string.
* A shortened string is returned, which must be freed by the caller.
*/
char* delete_char_from_string(const char* str, const char ch)
{
/* TODO */
return NULL;
}
/*
* Reverses the given string, without affecting the string.
* A reversed string is returned, which must be freed by the caller.
*/
char* reverse_string(const char* str)
{
/* TODO */
return NULL;
}
Last edited by dwhitney67; 06-04-2011 at 11:11 AM.
m sorry if i am completely wrong in my knowlegde that initializing a character pointer like:
Code:
char *name;
name="asddf";
ensures just the allocation of just the right amount of memory space for storing the string "asddf" (or whatever)..is it illegal to use the same method to take a user input as..
Code:
char *name;
scanf("%s",name);
???please help..
Last edited by mecrazyme1234; 06-05-2011 at 12:46 AM.
m sorry if i am completely wrong in my knowlegde that initializing a character pointer like:
[code]
char *name;
name="asddf";
[\code]
ensures just the allocation of just the right amount of memory space for storing the string "asddf" (or whatever)..is it illegal to use the same method to take a user input as..
[code]
char *name;
scanf("%s",name);
[\code]
???please help..
You used backslashes instead or forward slashes in the code tags.
This:
Code:
name = "asddf"
doesn't load the string "asddf" into the address pointed to by "name". Instead, it pre-allocates a buffer containing "asddf", and then changes the variable "name" to point to that location.
The "name" varaible doesn't contain a valid address (or it could contain one that's valid but doesn't belong to you) until you change it to point to one. If you really wanted to copy the string "asddf" into "name" instead of changing the location to which "name" points to, do this (actually the first example does change where "name" points to, but not while writing the string data to it):
Code:
char *name;
name = malloc(sizeof(char) * 6); // tell the OS to allocate 6 chars worth of memory in the heap to this process, and set "name" to point to it
memcpy(name, "asddf", sizeof(char) * 6); // copy the string "asddf" into the block pointed to by "name".
or this:
Code:
char name[6]; // pre-allocate 6 chars in a row on the stack
memcpy(name, "asddf", sizeof(char) * 6); // copy the string "asddf" into the block pointed to by "name".
is it illegal to use the same method to take a user input as..
[code]
char *name;
scanf("%s",name);
[\code]
neither law enforcement will put you in jail nor will the internet police come to confiscate your PC. But as long as you use this code, your program will cause segmentation faults.
Note that it is ok to use your method of initialization to make a pointer point to a known constant string. But no string that you read at runtime will ever be considered constant in this way, as its length is not known at compile time and it will not be linked into your application. And as MTK358 explains, this "initialization" of the pointer mypointer is not getting you the memory, but is just storing the address of "Hello World!" in your pointer variable. The memory just was there, because the compiler knew at compile time that this application would need it at runtime.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.