Note that there are some explanatory texts on larger screens.

plurals
  1. POArray of pointers implementation in C
    primarykey
    data
    text
    <p>Hii ,</p> <p>I have been trying to write a program... We have a structure which has a rank field and the name field.The pointer to this structure is stored in an array of fixed size. I have implemented it as follows and i have certain problems... The code i have written is :</p> <pre><code> #include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; #include&lt;malloc.h&gt; typedef struct { int rank; char *name; }node; int insert(node **a , char name[] , int *rank) { if(*rank &gt;= 5) { printf("\n Overflow "); return 0; } (*rank)++; node *new = (node *)malloc(sizeof(node)); new-&gt;name = name; new-&gt;rank = *rank; a[*rank] = new; return 0; } int delete(node **a , int *rank) { int i = *rank; if(*rank&lt;0) { printf("\n No elements"); return 0; } printf("\n Deleting %d , %s ",((a[*rank]))-&gt;rank,((a[*rank]))-&gt;name); printf("\n Reordering the elements "); while(i&lt;5) { a[i] = a[i+1]; } return 0; } int display(node **a , int rank) { while(rank&gt;0 &amp;&amp; (a[rank])&gt;0) { printf(" rank = %d name = %s \n",((a[rank])-&gt;rank),((a[rank])-&gt;name)); rank--; } return 0; } int main() { node *a[5] = {NULL}; char ch = 'y'; int choice,rank = -1; char name[10]; while(ch!='n' || ch!= 'N') { printf("\n Enter 1 to insert , 2 to delete , 3 to display and 4 to exit \n"); scanf("%d",&amp;choice); switch(choice) { case 1: printf("\n Enter name to insert"); gets(name); insert(a,name,&amp;rank); break; case 2: printf("\n Enter rank to delete "); scanf("%d",&amp;rank); delete(a,&amp;rank); break; case 3: display(a,rank); break; case 4: exit(0); default: printf("\n Invalid choice...please enter again "); break; } ch = getchar(); } return 0; } </code></pre> <p>First thing is the system automatically takes the choice except for the first time...(i couldn't find the fault there...) and i am a bit confused about this pointer stuff...Please see if its alright...Any corrections are welcome and please give me some explanation as to why it is wrong and how we shd do it...</p> <p>Thank You</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.
 

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