| | |
Array of Pointers to structures...
![]() |
•
•
Join Date: Oct 2009
Posts: 1
Reputation:
Solved Threads: 0
How to pass array of pointers to structures using functions in C?
#include<stdio.h>
#define dept_count 3
struct employee
{
char name[20],
post[20];
int emp_no;
int basic_pay;
};
void search( struct employee * , char , int );
void main()
{
struct employee *deptt[dept_count];
int i,j,emp_count[10],dep_srch;
char emp_srch[20];
clrscr();
for(i = 0 ; i < dept_count ; i ++)
{
printf("\nenter the number of employees in dept %d : ",i+1);
scanf("%d",&emp_count[i]);
for(j = 0 ; j < emp_count[i] ; j ++)
{
printf("\nEnter Employee Number : ");
scanf("%d",&deptt[i]->emp_no);
printf("\nEnter Employee's name : ");
scanf("%s",deptt[i]->name);
printf("\nEnter Employees post: ");
scanf("%s",deptt[i]->post);
printf("\nEnter Employees basic pay: ");
scanf("%d",&deptt[i]->basic_pay);
}
}
for(i = 0 ; i < dept_count ; i ++)
{
printf("\n\t\tDepartment %d: \n\n", i+1);
printf("\n\nEmp_no ");
printf("\tName ");
printf("\tPost ");
printf("\tBasic pay ");
for(j = 0 ; j < emp_count[i] ; j ++)
{
printf("\n\n%d",deptt[i]->emp_no);
printf("\t%s",deptt[i]->name);
printf("\t%s",deptt[i]->post);
printf("\t%d",deptt[i]->basic_pay);
}
}
printf("\n\nEnter the Deptt. No in which employee is to be searched:");
scanf("%d",&dep_srch);
printf("\nEnter the employee name:");
scanf("%s",emp_srch);
search(deptt[dep_srch],emp_srch[20],emp_count[dep_srch]);
getch();
}
void search (struct employee &dept , char name[], int emp_count)
{
int i,j;
for(i= 0 ; i < emp_count ; i++)
{
j = 0;
while(dept[i] == name[j] && name[j] != '\0')
{
j++;
}
if(name[j]=='\0')
{
printf("\n\tString found at index %d",i+1);
break;
}
}
if(name[j]!='\0')
printf("\n\tString not found....");
}
#include<stdio.h>
#define dept_count 3
struct employee
{
char name[20],
post[20];
int emp_no;
int basic_pay;
};
void search( struct employee * , char , int );
void main()
{
struct employee *deptt[dept_count];
int i,j,emp_count[10],dep_srch;
char emp_srch[20];
clrscr();
for(i = 0 ; i < dept_count ; i ++)
{
printf("\nenter the number of employees in dept %d : ",i+1);
scanf("%d",&emp_count[i]);
for(j = 0 ; j < emp_count[i] ; j ++)
{
printf("\nEnter Employee Number : ");
scanf("%d",&deptt[i]->emp_no);
printf("\nEnter Employee's name : ");
scanf("%s",deptt[i]->name);
printf("\nEnter Employees post: ");
scanf("%s",deptt[i]->post);
printf("\nEnter Employees basic pay: ");
scanf("%d",&deptt[i]->basic_pay);
}
}
for(i = 0 ; i < dept_count ; i ++)
{
printf("\n\t\tDepartment %d: \n\n", i+1);
printf("\n\nEmp_no ");
printf("\tName ");
printf("\tPost ");
printf("\tBasic pay ");
for(j = 0 ; j < emp_count[i] ; j ++)
{
printf("\n\n%d",deptt[i]->emp_no);
printf("\t%s",deptt[i]->name);
printf("\t%s",deptt[i]->post);
printf("\t%d",deptt[i]->basic_pay);
}
}
printf("\n\nEnter the Deptt. No in which employee is to be searched:");
scanf("%d",&dep_srch);
printf("\nEnter the employee name:");
scanf("%s",emp_srch);
search(deptt[dep_srch],emp_srch[20],emp_count[dep_srch]);
getch();
}
void search (struct employee &dept , char name[], int emp_count)
{
int i,j;
for(i= 0 ; i < emp_count ; i++)
{
j = 0;
while(dept[i] == name[j] && name[j] != '\0')
{
j++;
}
if(name[j]=='\0')
{
printf("\n\tString found at index %d",i+1);
break;
}
}
if(name[j]!='\0')
printf("\n\tString not found....");
}
0
#2 Oct 20th, 2009
Why didn't you use CODE TAGs? Information is posted all over this site about CODE tags, like
1) in the Rules you were asked to read when you registered
2) in the text at the top of this forum
3) in the announcement at the top of this forum titled Please use BB Code and Inlinecode tags
4) in the sticky post above titled Read Me: Read This Before Posting
5) any place CODE tags were used, even in responses to your posts
6) Even on the background of the box you actually typed your message in!
1) in the Rules you were asked to read when you registered
2) in the text at the top of this forum
3) in the announcement at the top of this forum titled Please use BB Code and Inlinecode tags
4) in the sticky post above titled Read Me: Read This Before Posting
5) any place CODE tags were used, even in responses to your posts
6) Even on the background of the box you actually typed your message in!
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1
#3 Oct 20th, 2009
•
•
•
•
How to pass array of pointers to structures using functions in C?
- A pointer variable is declared by appending * to the type.
- A pointer value is acquired by using the & unary operator.
C Syntax (Toggle Plain Text)
int obj = 5; int* p = &obj; /* p is a pointer to int and refers to obj's adress */
The two rules are recursive within limits, so you can add another level of indirection and get a pointer to a pointer:
C Syntax (Toggle Plain Text)
int obj = 5; int* p = &obj; int** pp = &p; /* &&obj will not work because &obj is not an object with an address */
Arrays are an anomaly on the surface because they have weird rules until you learn that in almost all cases, an array decays into a pointer. If you have an array
int arr[10]; then in all but two general cases, any use of arr will be equivalent to &arr[0] . That is why arrays can be changed when passed to a function. Pass-by-reference is simulated by default. It is also why the sizeof operator does not work on array parameters: C Syntax (Toggle Plain Text)
#include <stdio.h> void Function(char a[]) { printf("%lu\n", (unsigned long)sizeof a); /* will not print 20 */ a[0] = 'B'; } int main() { char a[20] = "Test"; printf("%lu\n", (unsigned long)sizeof a); /* prints 20 */ Function(a); puts(a); /* prints "Best" instead of "Test" */ return 0; }
C Syntax (Toggle Plain Text)
void Function(char *a) { printf("%lu\n", (unsigned long)sizeof a); /* will not print 20 */ a[0] = 'B'; }
C Syntax (Toggle Plain Text)
int arr[10]; /* array of 10 int */ int* arr[10]; /* array of 10 pointers to int */
C Syntax (Toggle Plain Text)
#include <stdio.h> void Function(int* a[], size_t sz) { size_t x; for (x = 0; x < sz; ++x) printf("%d\n", *a[x]); } int main() { int obj[] = {1, 2, 3, 4, 5}; int* ap[sizeof obj / sizeof *obj]; size_t x; for (x = 0; x < 5; ++x) ap[x] = &obj[x]; Function(ap, 5); return 0; }
C Syntax (Toggle Plain Text)
void Function(int** a, size_t sz) { size_t x; for (x = 0; x < sz; ++x) printf("%d\n", *a[x]); }
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> struct Test { int value; }; void TestFunction(struct Test* a[], size_t sz) { size_t x; for (x = 0; x < sz; ++x) { if (a[x]) printf("%d\n", a[x]->value); } } int main() { struct Test* test[5]; const size_t sz = 5; size_t x; for (x = 0; x < sz; ++x) { test[x] = malloc(sizeof test[x]); test[x]->value = x; } TestFunction(test, sz); for (x = 0; x < sz; ++x) free(test[x]); return 0; }
C Syntax (Toggle Plain Text)
void TestFunction(struct Test** a, size_t sz) { size_t x; for (x = 0; x < sz; ++x) { if (a[x]) printf("%d\n", a[x]->value); } }
int a[5][5]; is not the same type as int** a; when decaying. Only the first dimension decays into a pointer, so when decaying, int a[5][5]; is equivalent to int (*a)[5]; . This is a pointer to an array of 5 ints. The first dimension decaying rule is important to remember because it trips up a lot of programmers when they want to pass multidimensional arrays to functions.I did not go into all of the details where they are not relevant to your question, but this should be enough to help you with your code now and give you a good foundation in how pointers work for the future.
-Tommy (For Great Justice!) Gunn
![]() |
Similar Threads
- Passing 2D Array of Pointers into a function (C++)
- Dynamic array of pointers to structures - doubt (C++)
- double linked list array of pointers, on the right track? (C++)
- why i cann't execute my simple program(an array of pointers to object) (C++)
- select sort using array of pointers---getting wrong values. (C++)
- Array of pointers to objects (C++)
- Array of pointers (C++)
- Sorting structures (C)
Other Threads in the C Forum
- Previous Thread: void before main ()
- Next Thread: need help with atoi
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






