1,105,214 Community Members

Memory allocation problem

Member Avatar
Prankmore
Newbie Poster
14 posts since Jun 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
# include<stdio.h>
# include<string.h>

int

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);

else
    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?

Thanks

Member Avatar
Ancient Dragon
Achieved Level 70
27,573 posts since Aug 2005
Reputation Points: 5,231 [?]
Q&As Helped to Solve: 3,028 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

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
Member Avatar
Prankmore
Newbie Poster
14 posts since Jun 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Of course. I knew this. Thankyou very much

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