Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Lastest update:<br></strong> For testing: <br> please put the following code in your <code>main()</code>,and change the number 10 to the # of lines you have in the file. To compute the upper bound automatically you can try to change the return type of <code>get_phones</code> to <code>int</code> and put a counter in the while-loop.<br></p> <pre><code>int i=0; for(i=0;i&lt;10;i++){ printf("%lld\n",*(phones+i)); } </code></pre> <p><strong>You need deference a pointer in order to print the actual value. Otherwise you are printing the address stored in the pointer.</strong> And <code>phones</code>, besides being the name of an array, it is <strong>a pointer pointing to the first element in the array</strong> in its nature. For more info on pointer and array, see <a href="http://pw1.netcom.com/~tjensen/ptr/ch2x.htm" rel="nofollow">this tutorial</a>.</p> <p><strong>Assume that you are using a 32-bit machine</strong><br> The phone numbers I put in my textfile is the following:<br> 22121222345<br> 678139199<br> 111111111<br></p> <p><em>Note that 22,121,222,345>=2,147,483,647,</em> which is the maximum number an int can represent. (For <code>unsigned int</code> it is 2^32-1). Now if we try to run the following code:</p> <pre><code>void get_phones(int *phones){ FILE *fp; fp = fopen("phones.txt", "rt"); if (fp == NULL) printf ("Error\n"); else while (fscanf(fp, "%d\n", phones++) !=EOF){} } </code></pre> <p>The list of number printed on terminal are: <br> 646385865<br> 678139199<br> 111111111<br> <br> The first number saved in the array is totally random! Why? because it is too large for an integer and it overflows.</p> <p>Now if we try the following version:<br></p> <pre><code>void get_phones(long long* phones){ FILE *fp; fp = fopen("phones.txt", "rt"); if (fp == NULL) printf ("Error\n"); else while (fscanf(fp, "%lld\n", phones++) !=EOF){} } </code></pre> <p><br> The list of number printed on terminal are:<br> 22121222345<br> 678139199<br> 111111111<br> <br> Why does it work? Because the type <code>long long</code> can store number up to 2^63-1 = 9,223,372,036,854,775,807 <br> <br> Tested working version. Change the data type to <code>long long</code> if you want.<br><br></p> <p>!! The problem with your code is that you are passing the wrong thing to the <code>fscanf</code>. <code>phones</code> is already a pointer -- it is pointed to the starting point of the array. when you write <code>phone++</code>, it passes the current pointer to fscanf, and increase the pointer by one, which moves the pointer to the next slot in the integer array. <br><br>Also, although <code>fscanf</code> will return the number of items it scanned, in your while loop condition, the scanning is already performed. So you don't need to call it second time in the body of the while loop <br></p> <p>5195551234 this number is still too large for <code>unsigned long</code>, unfortunately. Because for 32-bit machine, both <code>int</code> and <code>unsigned long</code> has the max value $2^{32}-1$. Check out <a href="http://en.wikipedia.org/wiki/Integer_%28computer_science%29" rel="nofollow">wikipedia</a> if you need more information on this. Instead you need <code>long long</code> if you are running your code on 32-bit machine, otherwise it will overflow and store incorrect data. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload