Create an array of unknown strings - Undefined behaviour - arrays

In what way is the following code faulty or undefined behaviour?
I suggested this as a possibility to create an array of strings, if string number and size are unknown beforehand, and after a short discussion, it was suggested that I open a new question.
The following code produced the expected results when I compiled it with gcc, but that can happen in spite of undefined behaviour (it's undefined after all).
So, what is the mistake?
int n, i;
printf("How many strings? ");
scanf("%d", &n);
char *words[n];
for (i = 0; i < n; ++i) {
printf("Input %d. string: ", i + 1);
scanf("%s", &words[i]);
}
for (i = 0; i < n; ++i) {
printf("%s\n", &words[i]);
}
Edit:
I feel stupid now for missing this, I guess getting the correct answer back just made me miss my error. But that others may learn from my mistake:
I guess I got completely wrong what the & operator does. I thought it would give me where words points to, but of course it does the exact opposite. See the answers.

scanf("%s", &words[i]); and printf("%s\n", &words[i]); invokes *undefined behavior because data having wrong type are passed.
In both of scanf() and printf(), %s requires char* pointing at valid buffer but what is passed are char**.
Also don't forget to allocate buffer to store strings before reading.
Try this:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int n, i;
printf("How many strings? ");
scanf("%d", &n);
char *words[n];
for (i = 0; i < n; ++i) {
printf("Input %d. string: ", i + 1);
words[i] = malloc(1024001); /* allocate enough buffer */
if (words[i] == NULL) {
perror("malloc");
return 1;
}
scanf("%1024000s", words[i]); /* limit length to read to avoid buffer overrun */
}
for (i = 0; i < n; ++i) {
printf("%s\n", words[i]);
}
for (i = 0; i < n; ++i) {
free(words[i]); /* clean what is allocated */
}
return 0;
}

char *words[n];
creates an array of uninitialized pointers
scanf("%s", foo);
writes values to the position foo is pointing to
it is not specified where the pointers of words are pointing to so they could point anywhere which could result in a segfault
next words is a char**
words[i] is a char *
&words[i] is a char **
%s expects a char* so it's again undefined behavior what happens
so you first have to initialize your words arrays using for example malloc and then write the values to words[i]

This:
char *word;
is a pointer, Before it is used as a container for say a string, it needs to point to memory sufficient for the string.
for example, this will work
word = malloc(80*sizeof(*word));
if(word)
{//success, continue
Similar to above, this:
char *word[n];
extension is an an array of n pointers. Before any of the pointers can be used as a container for say some strings, each needs to point to its own memory location. Assuming the value n has been scanned in, this will work:
for(int i=0;i<n;i++)
{
word[i] = malloc(80*sizeof(*word[i]));//80 for illustration
if(!word[i])
{//handle error...
Once the memory is allocated, the strings can be populated.
However, to ensure user input does not overflow the buffer defined by each instance of word, use a width specifier in the format string:
Change:
scanf("%s", &words[i]);
To:
scanf("%79s", words[i]);//note & is not needed as the symbol for a char array serves as the address
// ^^ ^

Related

Why won't this array get printed?

I'm trying to write a program to take 10 words as input from the user, then store it an array, and then print out the length of each word.
Here's my code:
#include <stdio.h>
#include <string.h>
char *words[10];
char length[10];
int i, j;
int main()
{
printf("Input ten words: \n");
for(i = 0; i < 10; i++)
{
printf("Enter element %d \n", i + 1);
scanf("%s", &words[i]);
}
for(i = 0; i < 10; i++)
printf("%c", words[i]);
for(i = j = 0; j < 10; j++)
{
length[j] = strlen(words[i]);
i++;
}
for(j = 0; j < 10; j++)
printf("%c", length[j]);
return 0;
}
It should be noted that I have no idea why the array "words" is defined as a pointer, I only do it because if I don't I get some warning about making a pointer from integer without a cast.
When I run the program what happens is, I get prompted to input the 10 elements, that much works, but then when it's supposed to print the "words" array, the program just crashes.
Also the reason I coded it like this is because later on I also need to print the longest and shortest word - so I figured it would help if I had the lengths of all the strings in their own array.
Does anyone know what's wrong here?
Thanks
With the line char *words[10], you are declaring an array of 10 pointers. However, these pointers are uninitialized, which means they are wild pointers. Dereferencing a wild pointer causes undefined behavior (i.e. the program may crash). If you want to use these pointers in a meaningful way, you must make each pointer point to a valid memory location, for example to an address returned by the function malloc or to the address of a char array.
However, probably the easiest solution to your problem is to not use pointers at all, but to instead declare a two-dimensional char array, like this:
char words[10][100];
That way, you are allocating space for 10 words of up to 100 characters each (including the null terminating character).
Beware that a buffer overflow will occur if the user enters a word longer than 99 (1 byte is required for the terminating null character). Therefore, the scanf line should be changed to the following:
scanf("%99s", words[i]);
That way, scanf will never attempt to write more than 100 bytes (including the terminating null character).
I have also removed the & in the scanf line above, because the & is not necessary, since words[i] will automatically decay to &words[i][0].
Also, as a general rule, you should verify that the return value of scanf is 1 before attempting to use the value that scanf wrote. For example, if the user triggers end of file on the input stream (for example by pressing CTRL-D on Linux or CTRL-Z on Windows), then scanf will return -1 without writing anything into words[i]. In that case, by subsequently reading from words[i], your program will cause undefined behavior.
Additionally, the line
printf("%c", words[i]);
must be changed to:
printf("%s", words[i]);
The loop
for(i = j = 0; j < 10; j++)
{
length[j] = strlen(words[i]);
i++;
}
can be simplified to:
for(i = 0; i < 10; i++)
{
length[i] = strlen(words[i]);
}
The line
printf("%c", length[j]);
should probably be changed to
printf("%hhu", length[j]);
because length[j] does not represent the ASCII code of a character, but just a number.

Int array empty - C

I am attempting to write a program that will take two sets of strings N and Q. The goal of the program is to print out the number of times each string in Q occurs in N. However, I am struggling to manage strings and pointers in C and in particular I believe my issue stems from attempting to have an array of strings. I get a segmentation fault when the code below is executed. I have commented out my attempts at debugging using printf(). I believe the issue occurs when I try to assign S into the N_array.
int main() {
int N, Q;
char *N_array[1000], *Q_array[1000];
scanf("%d", &N);
for (int N_i = 0; N_i < N; N_i++) {
//printf("made it through A for loop %d times\n", N_i+1);
scanf("%s", N_array[N_i]);
}
scanf("%d", &Q);
//Does the array contain any information?
//for (int N_i = 0; N_i < N; N_i++) { printf("N_array[%d] == %d\n", N_i, N_array[N_i]);}
for (int Q_i = 0; Q_i < Q; Q_i++) {
//printf("Made it to B for loop\n");
int occurs = 0, result;
char s[21];
scanf("%s", &s[21]);
strcpy(Q_array[Q_i], s);
for (int N_i2 = 0; N_i2 < N; N_i2++) {
//printf("Made it to C for loop\n");
result = strcmp(Q_array[Q_i], N_array[N_i2]);
if (result == 0) occurs++;
}
printf("%d", occurs);
}
return 0;
}
One problem is here
for (int N_i = 0; N_i < N; N_i++) {
//printf("made it through A for loop %d times\n", N_i+1);
scanf("%s", N_array[N_i]);
}
N_Array contains 1000 pointers to char, but each of those pointers points to, well.. nowhere. It's an uninitialized pointer which points to a random memory location you don't own. This is undefined behavior. You have to allocate memory before scanf.
N_Array[N_i] = malloc(max_length_of_string + 1);
Another problem is this line
char s[21];
scanf("%s", &s[21]);
The second parameter of scanf should be just s, not &s[21], which is just outside your array.
And one line below you have the same problem as described in my first point
strcpy(Q_array[Q_i], s);
Q_array[Q_i] doesn't yet point to any memory which you're allowed to write to. You should allocate memory here as well.
scanf("%s", N_array[N_i]);
This should lead to SegFault as expected as N_array[N_i] doesn't contain a valid memory address.
What you need is to allocate memory using malloc, calloc to point N_array[N_i] to a valid memory location.
Correct code would be:
for (int N_i = 0; N_i < N; N_i++)
{
if(!(N_array[N_i] = malloc((MAX_STRING_LENGTH + 1) * sizeof(char))))
{
printf("malloc failed!\n);
exit(1);
}
scanf("%s", N_array[N_i]);
}
scanf("%s", &s[21]);
This is incorrect as %s expects char * but type of &s[21] is char **.
You need to use scanf("%20s", s); instead.
This way scanf only read at most 20 chars (as we have char s[21]. So leaving last byte for \0 character) and thus this will avoid accidental buffer overrun (where the length of string entered by user is greater than 20).
strcpy(Q_array[Q_i], s);
Again here Q_array[Q_i] points to invalid memory address as it contains garbage value. Use malloc, calloc etc. to allocate memory first.
Correct code could be:
if(!(Q_array[Q_i] = malloc((MAX_STRING_LENGTH + 1) * sizeof(char))))
{
printf("malloc failed!\n);
exit(1);
}
strcpy(Q_array[Q_i], s);

C: Format %s expects argument of type char* in funny strings program

I am making a program to check for funny strings. To understand the exercise read this.
My code is here:
#define MAX_STR_LENGTH 10
char* reverse(char *str) {
char *reversedStr = malloc(strlen(str));
for (int i = 0; i < MAX_STR_LENGTH; i++) {
for (int j = MAX_STR_LENGTH - 1; j > 0; j--) {
reversedStr[i] = str[j];
}
}
return reversedStr;
}
int isFunny(char *str, char *reversedStr) {
for (int i = 1; i < MAX_STR_LENGTH; i++) {
if (str[i] - str[i - 1] != reversedStr[i] - reversedStr[i - 1]) {
return 0;
}
}
return 1;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int numberOfStrings;
scanf("%i", &numberOfStrings);
char **strings;
for (int i = 0; i < numberOfStrings; i++) {
scanf("%s", &strings[i]);
}
char **reversedStrings;
for (int i = 0; i < numberOfStrings; i++) {
reversedStrings[i] = reverse(strings[i]);
}
for (int i = 0; i < numberOfStrings; i++) {
if (isFunny(strings[i], reversedStrings[i])) {
printf("Funny\n");
}
printf("Not funny\n");
}
return 0;
}
The error I am getting is the following:
solution.c: In function 'main':
solution.c:35:9: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char **' [-Wformat=]
scanf("%s", &strings[i]);
I don't understand why though.
My aim was to make an array of strings, called strings, and store there all the strings that I read. Why is strings[i] a char** ?
I would appreciate any help or tips.
Thanks!
Apparently you have a char * and you are passing it's address, which is wrong, scanf() wants a char pointer for each "%s" specifier, and the fix for your code is to use
char string[10];
scanf("%s", string);
the array automatically becomes a char pointer when passed to scanf() like above, but that's not enough.
Your array of char now as I suggest it is of fixed size, and even if it wasn't, using scanf() like that is dangerous, suppose that each array in the array of char arrays has 10 elements, then each string you want to read must have only 9 characters, you can instruct scanf() to stop reading when it already read 9 characters like this
scanf("%9s", strings[i]);
If you don't do this, then reading extra characters will be possible, but illegal, causing what's called undefined behavior.
If you want an array of strings, say of 5 strings with 9 characters each, then you can try
char strings[5][10];
int i;
for (i = 0 ; i < 5 ; ++i)
scanf("%9s", strings[i]);
Note: You need the & address of operator for example, when you pass an int value, because scanf() modifes the data pointed to by the passed pointer, so you need to make a pointer to the int variable where you want to scan the value, for that you use the & address of operator, since you pass a pointer containing the address of the variable.
Suggestion: Always check the return value of malloc(), it returns NULL on failure, and dereferencing a NULL poitner, is undefined behavior, you must be careful not to cause undefined behavior because it's too hard to debug.

Array of a struct with strings

I have defined a structure
struct subject
{
char name[100];
int year;
};
and since I need n of these and I have to use malloc I did the following in my main function:
int n, i;
scanf("%d", &n);
struct subject *ptr = malloc(n*sizeof(struct subject));
Unfortunately when I try to input something with this code:
for(i = 0; i < n; i++)
{
gets((ptr + i)->name);
scanf("%d", (ptr + i)->year);
}
It crashes after I type the first name. The task requires the use of malloc.
Here's the whole code (unfortunately it's in my native language so it's a little bit different)
#include <stdio.h>
#include<stdlib.h>
#ifndef DEBUG
#define DEBUG(...)printf(_VA_ARGS_)
#endif
struct kolegij
{
char naziv[100];
int semestar;
};
int main(){
int brPredmeta, i;
scanf("%d", &brPredmeta);
struct kolegij *ptr = malloc(brPredmeta*sizeof(struct kolegij));
if(ptr == NULL)
{
printf("error\n");
return 0;
}
for(i = 0; i < brPredmeta; i++)
{
//gets(ptr->naziv);
gets((ptr + i)->naziv);
scanf("%d", &(ptr + i)->semestar);
getchar();
}
for(i = 0; i < brPredmeta; i++)
{
printf("%s\n", ptr[i].naziv);
printf("%d\n", ptr[i].semestar);
}
return 0;
}
With regards to the duplicate issue. I believe this shouldn't be a duplicate since it's related to structs and pointers. I had issues with scanfs before and I haven't considered this as a solution so I think it shouldn't be flagged as a duplicate.
I believe the statement scanf("%d", (ptr + i)->year); to be responsible for your unstable behaviour, as you're passing an int where scanf expects an int *. Perhaps you meant something like scanf("%d", &(ptr + i)->year) (note the additional ampersand), though this also has perils associated. Namely, you're discarding the return value, so you can never be sure that the value stored will be sane. If the user enters invalid input, it might be best to re-prompt or exit:
int n;
do {
puts("Enter year: ");
n = scanf("%d", &(ptr + i)->year));
scanf("%*[^\n]"); // more on this later
getchar();
} while (n == 0);
if (n < 0) exit(EXIT_FAILURE);
As part of handling input is expecting the enter keystroke, we need to clear that before the next gets call, and I use scanf("%*[^\n]") followed by getchar() for that.
On the topic of the code which you presented near the end, that does indeed look very different. I notice you fixed the scanf type error (the additional ampersand), and you have getchar() after the scanf("%d", ...) so you won't usually get the trailing newline hanging around. I see no reason to be suspect of that (code at the end of your post); if you're concerned about a reason for closure, it appears that you don't have a problem anymore (we can't reproduce your crash)... I mean, aside from using gets; that's a minor problem, I guess (gets is deprecated; use fgets instead, trim the trailing '\n' and if there is none use scanf("%*[^\n]") followed by getchar() again to effectively truncate the input).
Malloc returns a void pointer type. In order to use it as a pointer to your structure you have to cast it first. Try changing the line with malloc into:
int n, i;
scanf("%d", &n);
struct subject *ptr = (struct subject*) malloc(n*sizeof(struct subject));
You also have to change your input into:
for(i = 0; i < n; i++)
{
gets((ptr + i)->name);
scanf("%d", &((ptr + i)->year));
}

How to input a string in a 2d array in C?

If i want to take an input in a 2d array with each string in one row, and the next in the other(i.e. change the row on pressing enter). How can i do that in C. C doesnt seem to have convenient "String" Handling. I obviously mean doing so without the use of getchar().
3 ways are there which are mentioned below.
If you know the maximum number of strings and maximum number of chars, then you can use the below way to declare a 2D character array.
char strs[MAX_NO_OF_STRS][MAX_NO_CHARS] = {0};
for (i = 0; i < MAX_NO_OF_STRS; i++)
{
scanf("%s", strs[i]);
}
If you know the maximum number of strings, and you dont want to waste the memory by allocating memory for MAX_NO_CHARS for all strings. then go for array of char pointers.
char temp[MAX_NO_CHARS] = {0};
char *strs[MAX_NO_OF_STRS] = NULL;
for (i = 0; i < MAX_NO_OF_STRS; i++)
{
scanf("%s", temp);
strs[i] = strdup(temp);
}
If you know the maximum number of strings during run time means, you can declare a double pointer of char. Get the number of strings n from user and then allocate memory dynamically.
char temp[MAX_NO_CHARS] = {0};
char **strs = NULL;
int n = 0;
scanf("%d", &n);
strs = malloc(sizeof(char*) * n);
for (i = 0; i < n; i++)
{
scanf("%s", temp);
strs[i] = strdup(temp);
}
#include<stdio.h>
main()
{
char student_name[5][25];
int i;
for(i=0;i<5;i++)
{
printf("\nEnter a string %d: ",i+1);
scanf(" %[^\n]",student_name[i]);
}
}
u can read strings using 2d array without using getchar() by putting space in scanf(" %[^\n]")
; before %[^\n]!
An alternative to using malloc and filling up an array of pointers with buffers of a fixed size, would be to allocate a 2d array (in static storage or on the stack) and fill it up. KingsIndian modifed code example would than look like this:
#include <stdio.h>
int main()
{
char str[2][256] = {{0}};
int i = 0;
for(i=0;i<2;i++)
{
scanf("%255s", &str[i][0]);
}
return 0;
}
If all strings you expect to get are no longer than some size, than this approach will spare you the need to deal with freeing the memory yourself. It is less flexible however, meaning that you can't fit the size of an individual buffer to the string it contains.
EDIT
Adding to the information in the comment, to read a string that is terminated only by a new-line, rather than by any whitespace:
scanf("%255[^\n]", str[i]);

Resources