-1

in my code i use malloc to create n string for my project and after that , i created 'tr' to put all of string from **str with small letters . and it give me an error :

Run-Time Check Failure #3 - The variable 'str' is being used without being initialized.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void main(void)
{
    int n;
    char **str;
    char *tr;
    int cnt, k;
    cnt = k = NULL;
    printf("Enter number fo strings:\n");
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
        str[i] = (char*)malloc(sizeof(char)*n);

    str = (char**)malloc(sizeof(char)*n + 1);

    puts("Enter The strings");
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; i < n; j++)
            scanf("%s", &str[i][j]);
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; i < n; j++)
        {
            if (str[i][j] >= 'a' && str[i][j] <= 'z')
                cnt++;
        }
    }

    tr = (char*)malloc(sizeof(char)*(cnt + 1));
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; i < n; j++)
        {
            if (str[i][j] >= 'a' && str[i][j] <= 'z')
                tr[k++] = str[i][j];
        }
    }

    tr[k] = NULL;
    puts(tr);
    free(tr);
    free(str);
}
Swordfish
  • 12,971
  • 3
  • 21
  • 43
  • 4
    You're doing exactly what it says you're doing. Allocate `str` before using `str[i]`. Move that allocation line up *before* you use it. Also *don't* cast `malloc`. – tadman Jan 18 '19 at 17:55
  • You first do something like `str[i] = malloc...` and on the line below you do something like `str = (char**)malloc(sizeof(char)*n + 1);` – hetepeperfan Jan 18 '19 at 17:55
  • `NULL` is a null *pointer*. It's not supposed to be used for other values. If you want to initialize `cnt` and `k` to zero, then use zero (`0`). – Some programmer dude Jan 18 '19 at 17:56

3 Answers3

3
char **str;
char *tr;
int cnt, k;
cnt = k = NULL;
printf("Enter number fo strings:\n");
scanf("%d", &n);
for (int i = 0; i < n; i++)
    str[i] = (char*)malloc(sizeof(char)*n);  // here

you use str which is uninitialized. What do you expect? str has indeterminate value since it is uninitialized and almost certainly holds an invalid pointer value. Dereferencing an invalid pointer (which the operator [] does) is undefined behaviour.

What you'd have to do is first allocating memory for str to hold the pointers to the strings.

Btw.

puts("Enter The strings");
for (int i = 0; i < n; i++)
{
    for (int j = 0; i < n; j++)
        scanf("%s", &str[i][j]);
}

Is also incorrect. There is no need for the inner loop to read input from stdin and store it in str[i]. Also, don't use %s without specifying a with to limit the number of characters written to the destination.

if (str[i][j] >= 'a' && str[i][j] <= 'z')

That's what islower() from <ctype> is for.

free(str);

That's not enough to deallocate the memory since str points to a number of pointers to char which also point to allocated memory.

for (size_t i = 0; i < n; ++i)
    free(str[i]);
free(str);

to the rescue.

Swordfish
  • 12,971
  • 3
  • 21
  • 43
1

You have these two sections of code in the wrong order:

for (int i = 0; i < n; i++)
    str[i] = (char*)malloc(sizeof(char)*n);

str = (char**)malloc(sizeof(char)*n + 1);

You need to allocate memory for str before you can assign to str[i]. And it should be sizeof(char *) * n; there's no need to add 1 to this (you only need this in tr, since you add a null terminator at the end).

It should be:

str = malloc(sizeof(char*)*n);
for (int i = 0; i < n; i++) {
    str[i] = malloc(sizeof(char)*n);
}

After

Also, please read Do I cast the result of malloc? and Why is it considered a bad practice to omit curly braces?

Barmar
  • 741,623
  • 53
  • 500
  • 612
1

You're not allocating before using, you're using then allocating, but also allocating incorrectly. The correct allocation is:

str = malloc(sizeof(char*) * n);

Where str is an array of char*, not an array of char.

It's good that you're listening to your compiler warnings, but this one was really specific and you should be able to find the problem. Ordering of allocations is extremely important and "close enough" is not acceptable. It either works, or it's undefined behaviour.

tadman
  • 208,517
  • 23
  • 234
  • 262