I wrote a C program which takes a string input from user and prints it on the screen.
int main (void)
{
char* string;
scanf("%s", string);
printf("%s", string);
return 0;
}
But the output was undesirable.
I gave input foo. Program printed out (null). Can anyone explain Why?
char* string;
This is just a pointer to a char. It doesn't point to any memory yet. You need to allocate memory for the characters that you want it to hold.
You can allocate memory using malloc()
#include<stdio.h>
#include<stdlib.h>
int main(void){
char *string;
string = malloc(100); //string of length 100
if(string == NULL){
printf("Error\n"); //if malloc() fails
}
if((scanf("%99s", string)) != 1){ //if scanf() fails
printf("Error, Fix it!\n");
exit(1);
}
printf("%s\n",string);
free(string);
return 0;
}
Or, use an array
#include<stdio.h>
#include<stdlib.h>
int main(void){
char string[100];
if((scanf("%99s", string)) != 1){
printf("Error, Fix it!\n");
exit(1);
}
printf("%s\n",string);
return 0;
}
scanf doesn't allocate any memory for string; you need to do this yourself prior to calling the function. Else your program behaviour is undefined.
A simple way would be to refactor to char string[100]; say, and hope that scanf doesn't attempt to read more than 99 characters plus the null terminator.
Later on, you'll realise that hoping is too much to ask, and will end up writing your own I/O functions.
You did not allocate memory to string. Thus, by writing into uninitiated pointer, you invoke undefined behavior.
Before you use string, either you have to allocate memory dynamically using malloc() or family, or define string to be an array, like char srting[32] or something.
Also, FWIW, it is always better to limit your input size by the allocated memory, like
scanf("%31s", string);
to protect from buffer overflow in case of longer-than-expected input.
Related
Function to calculate string length; I think the logic here is correct:
int strlength(char *s)
{
int count=0;
while(*s!='\0')
{
count++;
s++;
}
return count;
}
int main(void) {
char *s;
int length;
printf("enter your string ");
scanf("%s",s);
length = strlength(s);
printf("string length:%d",length);
return 0;
}
Am I taking the string correctly here and assigning it? Can anyone please explain why I get segmentation fault here?
Your s has to point to something. Here is an example of allocating 128 bytes:
s = malloc(128);
Be sure to release the memory once you're done with it:
free(s);
Note that you must limit how much can be read from the user.
scanf("%127s", s);
I've left an extra byte for the NUL terminator.
this second line of the main() function is declaring a pointer. However, that pointer is never initialized to point to some memory block that the program owns.
Suggest using one of the heap allocation functions (malloc, calloc, realloc) to initialize that pointer.
Using that uninitialized pointer is undefined behavior and (as you saw) leads to a seg fault event.
when calling any of the scanf() family of functions,
always check the returned value (not the parameter values) to assure the operation was successful.
when using the '%s' input/format specifier, always include a MAX
CHARACTERS modifier that is one less than the length of the input
buffer to avoid any buffer overflow. Such overflow is undefined
behavior and can lead to a seg fault event.
I am getting a runtime error in this and i can't seem to figure it out.
This is my code to reverse a string and change A to T and vice-versa and to change C to G and vice-versa,
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char* reverse(char *input)
{
int len=strlen(input);
char *rev=NULL;
rev=(char*)malloc(sizeof(char)*len);
int i,k=0;
for(i=len-1;i>=0;i--)
{
rev[k++]=input[i];
if(rev[k-1]=='A')
rev[k-1]='T';
else if(rev[k-1]=='T')
rev[k-1]='A';
else if(rev[k-1]=='C')
rev[k-1]='G';
else if(rev[k-1]=='G')
rev[k-1]='C';
}
rev[k]='\0';
return rev;
}
int main()
{
char *input=(char *)malloc(sizeof(char)*1000);
scanf("%s",input);
char *str =NULL;//(char*)malloc(sizeof(char)*1000);
str=reverse(input);
printf("%s",input);
free(input);
}
You do not allocate enough memory to hold your reversed string:
int len=strlen(input);
...
rev=(char*)malloc(sizeof(char)*len);
...
rev[k]='\0';
You missed 1 extra byte for the terminating \0.
And by the way... Please don't cast the return value from malloc to a pointer.
First of all, you use i++ instead of i-- in the for loop. That means that the for loop never ends and k gets very very big and your rev[k] atempts to access values that it shouldnt.
Secondly, you do not need to allocate memory for str, because you allocate it in the function.
And you should allocate memory for one more char in the function as you will need if for '\0'.
This is just a small program I wrote to find a problem with a larger one. Everything changes when I add the line with scanf. I know it is not safe, I read other threads concerning printf errors that suggest other functions. Anything but cin is fine. Btw, I didn't choose the type definitions of the 'messages', that came from my teachers, so I cannot change them.
#include <stdio.h>
#include <string.h>
char message1 [] = "amfdalkfaklmdklfamd.";
char message2 [] = "fnmakajkkjlkjs.";
char initializer [] = ".";
char* com;
char* word;
int main()
{
com = initializer;
int i = 1;
while (i !=4)
{
printf ("%s \n", com);
scanf("%s",word);
i++;
};
return 0;
}
The problem: after a single iteration the program exits, nothing is printed.
The reason the scanf will crash is buffer is not initialized: word has not been assigned a value, so it is pointing nowhere.
You can fix it by allocating some memory to your buffer, and limiting scanf to a certain number of characters, like this:
char word[20];
...
scanf("%19s", word);
Note that the number between % and s, which signifies the maximum number of characters in a string, is less by 1 than the length of the actual buffer. This is because of null terminator, which is required for C strings.
com is a pointer whose value is the address of the literal string initializer. Literal strings are contained within read-only memory areas, but the scanf function will attempt to write into the address given to it, this is an access-violation and causes the OS to kill your process, hence the crash you're seeing.
Change your scanf code to resemble this, note the addition of width limit in the %s placeholder, as well as the use of the scanf_s version to ensure there is no buffer overflow.
static int const BufferLength = 2048; // 2KiB should be sufficient
char* buffer = calloc( BufferLength , 1 );
if( buffer == null ) exit(1);
int fieldCount = scanf_s("%2047s", buffer, BufferLength );
if( fieldCount == 1 ) {
// do stuff with `buffer`
}
free( buffer );
Note that calloc zeroes memory before returning, which means that buffer can serve as a null-terminated string directly, whereas a string allocated with malloc cannot (unless you zero it yourself).
word has no memory associated with it.
char* word;
scanf("%s",word);
Could use
char word[100];
word[0] = '\0';
scanf("%99s",word);
If available, use getline().
Although not standard C, getline() will dynamicaly allocate memory for arbitrarily long user input.
char *line = NULL;
size_t len = 0;
ssize_t read;
while ((read = getline(&line, &len, stdin)) != -1) {
printf("%s", line);
}
free(line);
Linux Programmer's Manual GETLINE(3)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(){
char *s;
printf("enter the string : ");
scanf("%s", s);
printf("you entered %s\n", s);
return 0;
}
When I provide small inputs of length up to 17 characters (for example "aaaaaaaaaaaaaaaaa") the program works perfectly fine but on providing inputs of larger lengths, it gives me a runtime error saying "main.c has stopped working unexpectedly".
Is there some problem with my compiler (codeblocks) or my pc (windows 7)? Or is it somehow related to the input buffer of C?
It's undefined behaviour as the pointer is uninitialized. There's no problem with your compiler but your code has problem :)
Make s point to valid memory before storing data in there.
To manage buffer overflow, you can specify the length in the format specifier:
scanf("%255s", s); // If s holds a memory of 256 bytes
// '255' should be modified as per the memory allocated.
GNU C supports an non-standard extension with which you don't have to allocate memory as allocation is done if %as is specified but a pointer to pointer should be passed:
#include<stdio.h>
#include<stdlib.h>
int main() {
char *s,*p;
s = malloc(256);
scanf("%255s", s); // Don't read more than 255 chars
printf("%s", s);
// No need to malloc `p` here
scanf("%as", &p); // GNU C library supports this type of allocate and store.
printf("%s", p);
free(s);
free(p);
return 0;
}
the char pointer is not initialized, you should dynamiclly allocate memory to it,
char *s = malloc(sizeof(char) * N);
where N is the maximum string size you can read, And its not safe to use scanf
without specifying the maximum length for the input string, use it like this,
scanf("%Ns",s);
where N same as that for malloc.
You are not allocating any memory to the character array so first try to get memory by calling malloc() or calloc(). then try to use it.
s = malloc(sizeof(char) * YOUR_ARRAY_SIZE);
...do your work...
free(s);
You need to allocate enough memory for buffer where your pointer will point to:
s = malloc(sizeof(char) * BUF_LEN);
and then free this memory if you do not need it anymore:
free(s);
You're not allocating memory for your string, and thus, you're trying to write in a non-authorized memory address. Here
char *s;
You're just declaring a pointer. You're not specifying how much memory to reserve for your string. You can statically declare this like:
char s[100];
which will reserve 100 characters. If you go beyond 100, it will still crash as you mentionned for the same reason again.
The problem is with your code .. you never allocate memory for the char *. Since, there is no memory allocated(with malloc()) big enough to hold the string, this becomes an undefined behavior..
You must allocate memory for s and then use scanf()(I prefer fgets())
#include"stdio.h"
#include"malloc.h"
int main(){
char *str;
str=(char*)malloc(sizeof(char)*30);
printf("\nENTER THE STRING : ");
fgets(str,30,stdin);
printf("\nSTRING IS : %s",str);
return 0;
}
The code in C to read a character pointer
#include<stdio.h>
#include<stdlib.h>
void main()
{
char* str1;//a character pointer is created
str1 = (char*)malloc(sizeof(char)*100);//allocating memory to pointer
scanf("%[^\n]s",str1);//hence the memory is allocated now we can store the characters in allocated memory space
printf("%s",str1);
free(str1);//free the memory allocated to the pointer
}
I was getting this problem. I tried this code below and it worked:
char *text;
scanf("%s", *&text);
I dont know how it worked. I just felt like doing it.
I'm fairly competent in a few scripting languages, but I'm finally forcing myself to learn raw C. I'm just playing around with some basic stuff (I/O right now). How can I allocate heap memory, store a string in the allocated memory, and then spit it back out out? This is what I have right now, how can I make it work correctly?
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char *toParseStr = (char*)malloc(10);
scanf("Enter a string",&toParseStr);
printf("%s",toParseStr);
return 0;
}
Currently I'm getting weird output like '8'\'.
char *toParseStr = (char*)malloc(10);
printf("Enter string here: ");
scanf("%s",toParseStr);
printf("%s",toParseStr);
free(toParseStr);
Firstly, the string in scanf is specifies the input it's going to receive. In order to display a string before accepting keyboard input, use printf as shown.
Secondly, you don't need to dereference toParseStr since it's pointing to a character array of size 10 as you allocated with malloc. If you were using a function which would point it to another memory location, then &toParseStr is required.
For example, suppose you wanted to write a function to allocate memory. Then you'd need &toParseStr since you're changing the contents of the pointer variable (which is an address in memory --- you can see for yourself by printing its contents).
void AllocateString(char ** ptr_string, const int n)
{
*ptr_string = (char*)malloc(sizeof(char) * n);
}
As you can see, it accepts char ** ptr_string which reads as a pointer which stores the memory location of a pointer which will store the memory address (after the malloc operation) of the first byte of an allocated block of n bytes (right now it has some garbage memory address since it is uninitialized).
int main(int argc, char *argv[])
{
char *toParseStr;
const int n = 10;
printf("Garbage: %p\n",toParseStr);
AllocateString(&toParseStr,n);
printf("Address of the first element of a contiguous array of %d bytes: %p\n",n,toParseStr);
printf("Enter string here: ");
scanf("%s",toParseStr);
printf("%s\n",toParseStr);
free(toParseStr);
return 0;
}
Thirdly, it is recommended to free memory you allocate. Even though this is your whole program, and this memory will be deallocated when the program quits, it's still good practice.
You need to give scanf a conversion format so it knows you want to read a string -- right now, you're just displaying whatever garbage happened to be in the memory you allocated. Rather than try to describe all the problems, here's some code that should at least be close to working:
char *toParseStr = malloc(10);
printf("Enter a string: ");
scanf("%9s", toParseStr);
printf("\n%s\n", toParsestr);
/* Edit, added: */
free(toParseStr);
return 0;
Edit: In this case, freeing the string doesn't make any real difference, but as others have pointed out, it is a good habit to cultivate nonetheless.
Using scanf() (or fscanf() on data you don't control) with a standard "%s" specifier is a near-certain way to get yourself into trouble with buffer overflows.
The classic example is that it I enter the string "This string is way more than 10 characters" into your program, chaos will ensue, cats and dogs will begin sleeping together and a naked singularity may well appear and consume the Earth (most people just state "undefined behaviour" but I think my description is better).
I actively discourage the use of functions that cannot provide protection. I would urge you (especially as a newcomer to C) to use fgets() to read your input since you can control buffer overflows with it a lot easier, and it's more suited to simple line input than scanf().
Once you have a line, you can then call sscanf() on it to your heart's content which, by the way, you don't need to do in this particular case since you're only getting a raw string anyway.
I would use:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFSZ 10
int main(int argc, char *argv[]) {
char *toParseStr = malloc(BUFFSZ+2);
if (toParseStr == NULL) {
printf ("Could not allocate memory!\n");
return 1;
}
printf ("Enter a string: ");
if (fgets (toParseStr, BUFFSZ+2, stdin) == NULL) {
printf ("\nGot end of file!\n");
return 1;
}
printf("Your string was: %s",toParseStr);
if (toParseStr[strlen (toParseStr) - 1] != '\n') {
printf ("\nIn addition, your string was too long!\n");
}
free (toParseStr);
return 0;
}
You don't need an & before toParseStr in scanf as it is already a pointer
also call free(toParseStr) afterwards
First, the errors that was keeping your program from working: scanf(3) takes a format-string, just like printf(3), not a string to print for the user. Second, you were passing the address of the pointer toParseStr, rather than the pointer toParseStr.
I also removed the needless cast from your call to malloc(3).
An improvement that your program still needs is to use scanf(3)'s a option to allocate memory for you -- so that some joker putting ten characters into your string doesn't start stomping on unrelated memory. (Yes, C will let someone overwrite almost the entire address space with this program, as written. Giant security flaw. :)
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char *toParseStr = malloc(10);
printf("Enter a short string: ");
scanf("%s",toParseStr);
printf("%s\n",toParseStr);
return 0;
}