IP string to integer [interview]

Source: Internet
Author: User

I went to the O & M department of an Internet company for an interview. Although I wrote a question in five minutes, there were many mistakes. Fortunately, I only pointed out one or two questions. Let alone run it! Recently, I was preparing to apply for a job. I used it last night for practice. I didn't pick it up for more than two hours. I went to bed more than 12 o'clock in the evening and got up and changed it, but it was still not completely correct. After breakfast today, I debugged it for another hour and finally succeeded! Share it!

First, sum up experience: 1. You must think carefully before writing, which is more time-saving than writing and modifying!

2. If the basic confirmation is a syntax error, you can adjust it slowly. If your logic is unclear, simply repeat it! Have the courage to refactor!

1 # include <stdio. h> 2 // The program function converts IP addresses such as "192.168.1.29" to an integer of 3 int main () (equivalent to the one after the decimal point is removed () {4 5 char ip [] = "0.3.2.1. "; // if your machine sizeof (int) is 2 bytes, consider overflow! 6 7 int sum = 0, I = 0, j = 0, n = 0; // erroor1: I is not initialized. A segment error occurs when the subscript is out of bounds! 8 int cnt = 4; 9 char tmp [4]; 10 11 while (cnt> 0) {12 int tmpsum = 0; // do not remember the start sum initialization, initialization is required for any new variable while writing; otherwise, the result is incorrect! 13 14 if (ip [j]! = '. ') {15 tmp [I] = ip [j]; 16 // printf ("% c \ n", ip [j]); 17 I ++; j ++; 18} else {19 I = 0; j ++; 20 tmpsum = atoi (tmp); 21 // printf ("* % d \ n ", tmpsum); 22 if (cnt! = 1) {23 for (n = cnt-1; n> = 1; n --) 24 tmpsum * = 256; 25 printf ("** % d \ n", tmpsum ); 26} 27 cnt --; 28} 29 30 sum + = tmpsum; 31 // printf ("*** % d \ n", tmpsum ); // 32} 33 printf ("% d \ n", sum); 34 return 0; 35}

 

I also learned how to debug the gdb program today. I was too lazy to debug the program without IDE. Today I see Chen Hao's articles in the "debug program with GDB" series, which I would like to recommend to you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.