1.11M Members

Memory allocation problem

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


main(int argc, char **argv){

char phone[16];
int num = 0;
int count = 0;

	printf("Enter a phone number: ");

count = scanf ( "%s%n", phone, num );

	memmove(phone + 3, phone + 2, 10);
	memset(phone + 3, '-', 1);
	memmove(phone + 7, phone + 6, 10);
	memset(phone + 7, '-', 1);

if (count == 1 && num == 10)

	printf("Number Is %s \n num is %d\n count is %d\n" ,phone, num, count);

    printf("Number Is Invalid\n");

return 0;

This is meant to take a user inputed phone number eg 5551239876

and return a hyphenated version eg 555-123-9876

and also make sure that it is a valid 10 digit phone number

when i try to run this i get an error

:19:1: warning: format ‘%n’ expects type ‘int *’, but argument 3 has type ‘int’

i don't really understand since i have allocated num as int and i expect num to be an int, can anybody shed some light?



scanf() expects the prameters to be pointers so that it can change the variables values. num is just an integer, not a pointer, so put the & pointer operator in front of it. count = scanf ( "%s%n", phone, &num ); Also see this article about %n.

Question Answered as of 2 Years Ago by Ancient Dragon

Of course. I knew this. Thankyou very much

This question has already been solved: Start a new discussion instead
Start New Discussion
View similar articles that have also been tagged: