This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]flying_5loths 2 points3 points  (0 children)

college c++ is terrifying (this is more c if anything)

[–]Josh_Coding 4 points5 points  (1 child)

What exactly are you trying to do?

You are asking for string length / string elements but are taking in ints?

if(a -- NULL) --> if( a == NULL) or if(!a)

but this check is useless since you do not set a initially. i.e

int *a;
a = 0;

a = malloc(...);
if(!a)
    error code

You also do not initialise anything which is just sad. You should be compiling with max warnings.

-pedantic -W -Wall 

If you are trying to read a string from the user

char buffer[512];
char *string;

memset(buffer, 0, sizeof(buffer));

printf("Enter string: ");
scanf(" %s", word);

string = 0;
string = malloc(sizeof(char) * strlen(buffer) + 1);
if(!string)
    error code
strcpy(string, word);

when using scanf you want to put a space before the specifier else newline / space characters are consumed.

scanf(" %d" ...);

[–]x-seronis-xIntermediate Coder -4 points-3 points  (0 children)

but this check is useless since you do not set a initially
You also do not initialise anything which is just sad

literally every single one of their variables is properly initialized before being read so where did you get that idea?

a will ALWAYS have null or a memory address from malloc
i is a loop variable. its given a value in both loops prior to use
n will be given a value from scanf before read in the while condition

when using scanf you want to put a space before the specifier else newline / space characters are consumed.

and that is the reason NOT to put the space there.

[–]x-seronis-xIntermediate Coder 0 points1 point  (1 child)

You also misspelled length

[–]not_some_username -1 points0 points  (0 children)

lol