Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>That code won't even <em>compile</em> as it stands, producing (in my environment):</p> <pre><code>pax@pax-desktop:~$ gcc -Wall -Wextra -o qq qq.c qq.c: In function ‘main’: qq.c:12:2: warning: implicit declaration of function ‘printf’ qq.c:12:2: warning: incompatible implicit declaration of built-in function ‘printf’ qq.c:14:4: warning: implicit declaration of function ‘gets’ qq.c:14:10: error: ‘addressbook’ has no member named ‘lastname’ qq.c:16:10: error: ‘addressbook’ has no member named ‘firstname’ qq.c:21:28: error: ‘addressbook’ has no member named ‘lastname’ qq.c:22:31: error: ‘addressbook’ has no member named ‘firstname’ qq.c:25:1: warning: control reaches end of non-void function </code></pre> <p>You should always (at least initially) compile with a high warning level and take note of what the compiler is telling you:</p> <ul> <li>you should include <code>stdio.h</code> if you're going to use <code>printf</code> and <code>gets</code>.</li> <li>you should use consistent field names in your structure.</li> <li>you should return something from non-void functions (technically this is not necessary under later versions of the standard but it's still a good idea for portability).</li> </ul> <p>In addition, you may want to consider the following:</p> <ul> <li><code>int main()</code> is not one of the two canonical forms of the <code>main</code> function. While the standard <em>allows</em> for implementation-defined extra ones, the "correct" one for this particular case would be <code>int main (void)</code>.</li> <li><code>gets</code> is a <em>dangerous</em> function since there is no way to prevent buffer overflow, making your code insecure. For example, if I entered a thousand characters for my first name, it may well screw up your program by overwriting huge chunks of accounting information on the stack (like return addresses for example). You can find a safer input function <a href="https://stackoverflow.com/questions/4023895/how-to-read-string-entered-by-user-in-c/4023921#4023921">here</a>.</li> </ul> <p>Making <em>most</em> of those changes, you would end up with something like:</p> <pre><code>#include &lt;stdio.h&gt; typedef struct { char lastname[25]; char firstname[20]; char address[20]; char phonenumber[20]; }addressbook; addressbook a; int main (void) { printf("enter details:\n"); printf("enter lastname:\n"); gets(a.lastname); printf("enter firstname:\n"); gets(a.firstname); printf("enter address:\n"); gets(a.address); printf("enter phone number:\n"); gets(a.phonenumber); printf("lastname:%s\n",a.lastname); printf("firstname: %s\n", a.firstname); printf("address:%s\n", a.address); printf("phone number:%s\n", a.phonenumber); return 0; } </code></pre> <p>which compiles and runs okay, although it still has the serious buffer overflow vulnerability. If you want to fix that, you can refer to the link I gave.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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