Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to approach and optimize code in C
    primarykey
    data
    text
    <p>I am new to C and very much interested in knowing how to approach any problem which has more than 3 or 4 functions, I always look at the output required and manipulate my code calling functions inside other functions and getting the required output. Below is my logic for finding a students record through his Id first &amp; then Username. This code according to my professor has an excessive logic and is lacking in many ways, if someone could assist me in how should I approach any problem in C or in any other language it would be of great help for me as a beginner and yes I do write pseudo code first.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; typedef struct{ int id; //Assuming student id to be unique int age; char *userName; //Assuming student userName to be unique char *dept; }student; // Alias "student" created for struct student* createstruct(); // All function prototype declared student* createArray(); void addstruct(student* s2); void searchChar(student* s2,int num); void searchInt(student* s2,int num); student* createstruct() // function createStruct() to malloc data of struct student. { student *s; s = (student*)malloc(sizeof(student)); s-&gt;userName = (char*)malloc(sizeof(char)*32); s-&gt;dept = (char*)malloc(sizeof(char)*32); printf("please enter id "); scanf("%d",&amp;s-&gt;id); printf("please enter age "); scanf("%d",&amp;s-&gt;age); printf("please enter userName "); scanf("%31s",s-&gt;userName); printf("please enter department "); scanf("%31s",s-&gt;dept); printf("\n"); return s; } student* createArray() { student *arr; //declaration of arr poiter, type struct student arr = (student*)malloc(sizeof(student)*10); // memory allocated for a size of 10 return arr; } void addstruct(student *s2) // function for adding data to the structures in array { int i,num; student* s1; printf("please enter the number of records to add:"); scanf("%d",&amp;num); printf("\n"); if(num&gt;0 &amp;&amp; num&lt;11) { for(i=0;i&lt;num;i++) // if user want to enter 5 records loop will only run 5 times { s1 = createstruct(); s2[i].id = s1-&gt;id; // traversing each element of array and filling in struct data s2[i].age = s1-&gt;age; s2[i].userName = s1-&gt;userName; s2[i].dept= s1-&gt;dept; } } else if(num&gt;10) // if user enters more than 10 { for(i=0;i&lt;10;i++) // loop will still run only 10 times { s1 = createstruct(); s2[i].id = s1-&gt;id; s2[i].age = s1-&gt;age; s2[i].userName = s1-&gt;userName; s2[i].dept = s1-&gt;dept; } printf("Array is full"); // Array is full after taking 10 records printf("\n"); } searchInt(s2,num); // Calling searchInt() function to search for an integer in records searchChar(s2,num); // Calling searchChar() function to search for a string in records free(s1); free(s2); } void searchChar(student* s2,int num) // function for searching a string in records of structure { char *c; int i; c = (char*)malloc(sizeof(char)*32); printf("please enter userName to search "); scanf("%31s",c); printf("\n"); for (i=0;i&lt;num;i++) //num is the number of struct records entered by user { if ((strcmp(s2[i].userName,c)==0)) //using strcmp for comparing strings { printf("struct variables are %d, %d, %s, %s\n", s2[i].id,s2[i].age,s2[i].userName,s2[i].dept); break; } else if(i == num-1) { printf("nothing in userName matches: &lt;%s&gt;\n",c); break; } } } void searchInt(student* s2,int num) //searchs for an integer and prints the entire structure { int i,z; printf("please enter id to search "); scanf("%d",&amp;z); printf("\n"); for (i=0;i&lt;num;i++) { if (s2[i].id == z) { printf("struct variables are %d, %d, %s, %s\n\n", s2[i].id,s2[i].age,s2[i].userName,s2[i].dept); break; } else if(i == num-1) { printf("nothing in id matches: &lt;%d&gt;\n\n",z); break; } } } int main(void) { student *s2; s2 = createArray(); addstruct(s2); return 0; } </code></pre>
    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.
 

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