Home > database >  resolving memory loss between two arrays in C
resolving memory loss between two arrays in C

Time:08-24

Good evening. I'm working on a program for class and I am hitting a brick wall when it comes to dealing with arrays using C.

--EDIT-- Full code has been posted.


#define _CRT_SECURE_NO_WARNINGS
#define STRMAX 20
#define MAX 100
#include<stdio.h>
int main()
{
    int count = 0;
    char strlist[STRMAX][MAX];
    int start = 0, end = STRMAX;
    for (start; start < end; start  ) {
        char string[MAX];
        printf("Enter a string: ");
        fgets(string, MAX - 1, stdin);
        printf("\nThe string is: %s", string);
        int size = strlen(string);
        int result = strcmp(string, "stop\n");
        if (result == 0) {
            break;

        }

        strcpy(strlist[start], string);
        count = count   1;
    }
    char rev[STRMAX][MAX];
    int temp = 0;
    printf("count is: %d\n",count);
    while (count != 0) {
        strcpy(rev[temp], strlist[count]);
        temp = temp   1;
        count = count - 1;
    }
    printf(rev);

   
    return 0;
}

The last line, printf(rev); is throwing the warning: "using uninitialized memory 'rev'. "

I do not understand C, its the beginning of this course. However I am NOT looking for a "do my homework for me" answer, more of a "here is a better way to go about this" answer.

the output for the code is:

Enter a string: 1

The string is: 1
Enter a string: 2

The string is: 2
Enter a string: 3

The string is: 3
Enter a string: stop

The string is: stop
count is: 3
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠3

the "count is: 3" is entirely for debugging. I really don't have a clue why my solution doesn't work. If there is any more information that is needed or anything else you would like to see feel free to ask and i'll update the post! thanks.

--EDIT-- STRMAX and MAX are both definitions set for the 2D array required for keeping an array of strings (20 and 50 respectively)

CodePudding user response:

First of all, the line

strcpy(rev[temp], strlist[count]);

is wrong. Valid indexes for strlist are 0 to count-1, assuming that you only want to read valid strings. However, you are using the indexes 1 to count instead. Therefore, you should move the line

count = count - 1;

before that line.

Also, the line

printf(rev);

does not make sense.

If you want to print all strings in the array, then you should print every string individually, in a loop.

Since you are storing the number of valid strings in the variable temp, you must print that many strings.

for ( int i = 0; i < temp; i   )
{
    printf( "%s\n", rev[i] );
}

Also, you should #include <string.h>, because you are using strcpy and strlen.

Additionally, you probably should remove the trailing newline character from the input obtained from fgets. Otherwise, you will be printing that newline character, which will give you unwanted extra lines, forcing you to compensate by printing less newline characters explicitly. The existance of the newline character is also forcing you to add a newline character to the target string "stop":

int result = strcmp(string, "stop\n");

You will be able to remove that newline character from the target string if you also remove it from the input string.

After making these changes, your code should look like this:

#define _CRT_SECURE_NO_WARNINGS
#define STRMAX 20
#define MAX 100
#include <stdio.h>
#include <string.h>
int main()
{
    int count = 0;
    char strlist[STRMAX][MAX];
    int start = 0, end = STRMAX;
    for (; start < end; start  ) {
        char string[MAX];
        printf("Enter a string: ");
        fgets(string, MAX - 1, stdin);

        //remove trailing newline character
        string[strcspn(string,"\n")] = '\0';

        printf("The string is: %s\n", string);
        int result = strcmp(string, "stop");
        if (result == 0) {
            break;
        }

        strcpy(strlist[start], string);
        count = count   1;
    }
    char rev[STRMAX][MAX];
    int temp = 0;
    printf("count is: %d\n",count);
    while (count != 0) {
        count = count - 1;
        strcpy(rev[temp], strlist[count]);
        temp = temp   1;
    }

    for ( int i = 0; i < temp; i   )
    {
        printf( "%s\n", rev[i] );
    }
    
    return 0;
}

This program has the following output:

Enter a string: 1
The string is: 1
Enter a string: 2
The string is: 2
Enter a string: 3
The string is: 3
Enter a string: stop
The string is: stop
count is: 3
3
2
1

CodePudding user response:

You are mistaking the syntax of printf(). printf("placeholder", variable) this is the proper way. "placeholder" depends on the variable type ofcourse.

  •  Tags:  
  • c
  • Related