#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
if (argc == 1)
exit(EXIT_FAILURE);
int count = 2;
int i, j;
for (i = 1; i < argc; i++)
{
for (j = 0; j < strlen(argv[i]); j++)
count++;
}
printf("%d\n", count);
char* original = malloc(sizeof(char) * count);
printf("%p\n", original);
char* copy = malloc(sizeof(char) * count);
printf("%p\n", copy);
memset(original, 0, strlen(original));
memset(copy, 0, strlen(copy));
strcpy(original, argv[1]);
for (i = 2; i < argc; i++)
{
strcat(original, argv[i]);
}
int coun = 0;
for (i = 0; i < strlen(original); i++)
{
if (original[i] == '(' || original[i] == '{' || original[i] == '[' ||
original[i] == ')' || original[i] == '}' || original[i] == ']')
{
copy[coun] = original[i];
coun++;
}
}
printf("%s\n", original);
printf("%s\n", copy);
free(original);
free(copy);
exit(EXIT_SUCCESS);
}
I used gcc -Wall -Werror -fsanitize=address balance.c -o balance to make file and ./balance '((' to test
and I got this message
what is the problem?
It is the code to get argv's contents and get only parenthesis on a string.
It might be error checking on -fsanitize=address I got it but I cannot find any errors on my code, so can someone check this please?
strlen(original)- what do you think it is supposed to return?originalis not a string at all. Same withstrlen(copy)stringin C is not a type. It is a content. That is an array terminated by a null-character. Your arrays are uninitialized.strxxxxfunctions on something which is not a string. You already know the sizes of the buffers though (sizeof(char) * count), you used them formalloc, so use the same formemsets.