Take for example this code:
char sentence[] = "";
scanf("", &sentence);
If I'm correct, a value will automatically be assigned to the array that is sufficient to accommodate the string that is entered by the user. Is there a way I can assign this value to a variable or something so that Ican recall it later? Basically what I want to do is I want to be able to 'read' the amount of letters the user has entered and use that value in a for loop
CodePudding user response:
Some things wrong here
char sentence[] = ""; <<<<=== buffer size 1 (\0)
scanf("", &sentence); <<<==== no format specifiers, do not pass address of string buffer, not big enough to receive a string
Should be
char sentence[50];
scanf("Is", sentence);
CodePudding user response:
@pm100 has covered the problems with your code presented. In terms of your title question, you can't recall anything once it's been overwritten. As you speculate, you'll need to save whatever you want elsewhere if you want to use it later. For example:
// most simply, make an initial assignment to one string, read another from
// the user
char sentence[50] = "my initial string"; // initial assignment
char sentence2[50];
scanf("Is", sentence2);
Or if you want the paradigm of saving an initial assignment:
#include <string.h>
...
char sentence[50] = "my initial string";
char origSentence[50];
strcpy(origSentence, sentence);
// read new sentence from the user
scanf("Is", sentence);
// this reads the string length, the amount of letters the user entered
size_t strLen = strlen(sentence);
for (size_t i=0; i<strLen; i )
{
// whatever you want to do here, the original string lives in
// origSentence, what the user entered lives in sentence
}
CodePudding user response:
As @pm100 said, you should pre-allocate a fixed amount of memory to your array. You do not know in advance what will be the amount of characters the user will give, so you should give an upper bound for its input. scanf has to be avoided wherever the environment is critical, due to buffer overflow.
You can get the size of the string of the user with strlen of the string standard library.
#include<stdio.h>
#include<string.h>
int main(int argc, char *argv[])
{
char sentence[50];
scanf("Is", sentence);
printf("%zu", strlen(sentence));
return 0;
}
CodePudding user response:
char sentence[] = "";
That creates an array containing one character, a null character that marks the end of a string.
Once created, the size of that array cannot be changed.
scanf("", &sentence);
This scanf
has no conversion specifier, like %s
, that tells it what to expect in input and how to store it. If it did have %s
, then &sentence
would be the wrong thing to pass it. For %s
, you should pass a pointer to char
, but the type of &sentence
is a pointer to an array of char
.
You could pass the correct type of pointer by passing sentence
, as in scanf("%s", sentence);
. Although sentence
is an array, it will be automatically converted to a pointer to its first element, hence a pointer to char
. However, this is still not useful as the array is not large enough to hold any string other than the string with no characters.
If I'm correct, a value will automatically be assigned to the array that is sufficient to accommodate the string that is entered by the user.
This is not correct. The standard C scanf
will not allocate memory for strings.
The GNU C Library and POSIX have an extension to scanf
in which you can use %ms
as a conversion specifier, and it will allocate space. In this case, you need to pass a pointer to a pointer to a char
, not a pointer to an array of char
:
char *sentence;
int result = scanf("%ms", &sentence);
if (result != 1)
{
fprintf(stderr, "Error, scanf did not work as desired.\n");
exit(EXIT_FAILURE);
}
Is there a way I can assign this value to a variable or something so that Ican recall it later?
After a successful call to scanf
using %ms
, you can find the length of the string that was read using strlen
, as in strlen(sentence)
.