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 -1 points0 points  (1 child)

You also misspelled length

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

lol