We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,987 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Memory allocation problem

# 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

2
Contributors
2
Replies
5 Hours
Discussion Span
1 Year Ago
Last Updated
3
Views
Question
Answered
Prankmore
Newbie Poster
14 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

Ancient Dragon
Achieved Level 70
Team Colleague
32,124 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 69
Question Answered as of 1 Year Ago by Ancient Dragon

Of course. I knew this. Thankyou very much

Prankmore
Newbie Poster
14 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1326 seconds using 2.69MB