Using malloc() within Multi-dimensional Arrays in C.
Multiple dimensions with arrays in C.
Date Created:Tuesday May 08th, 2007 04:10 PM
Date Modified:Sunday August 03rd, 2008 12:37 PM
/*
* Author: Dan Lynch
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void getname( char ** x, int z);
void searchname( char ** x, int n);
int main() {
char * name[4];
getname(name, 4);
searchname(name, 4);
return 0;
}
void getname( char ** x, int n) {
int i;
char ch[21];
for (i=0; i<n; i++) {
printf("Enter Name %d>",i);
gets(ch);
x[i]=malloc((strlen(ch)+1)*sizeof(char));
strcpy(x[i], ch);
}
}
void searchname( char ** x, int n) {
int i;
char * q;
printf("Type a word to search in the array:");
scanf("%s",q);
for (i=0; i<n; i++)
if (!strcmp(x[i],q)) printf("Match in element x[%d]!\n",i);
else printf("No match for %s\n",x[i]);
}
Downloads:
Download: code.c 718 B
Please login or Click Here to register for downloads
Multidimensional Arrays by Dan Lynch
is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
Based on a work at www.3daet.com
Permissions beyond the scope of this license may be available at http://www.3daet.com
