i cannot understand...how to start because we cannot use structures or records everything is done inside main function....

P.S. i am not asking for CODE!!!!...i want to kno how to declare and use such function.....thank you...first yr programmer!:S


how to create a 2d dynamic array of strings called book??????...where &book[*][0] stores last name, &book[*][0] stores first name and &book[*][50]....stores phone number...

first ask for number of users wanted
ex: 1
example: clinton bill 1234567891

i kno how to search and validate but i am stumped no how to not use structures or linked lists and in main function alone
doin this
how to create a 2d dynamic array of strings called book??????...where &book[*][0] stores last name, &book[*][0] stores first name and &book[*][50]....stores phone number...


en help would be appreciated!!!!!!!!!!!!!!!!...thank you

One way I can think of is to create a char *book; pointer then allocate memory as & when you need. You have to understand how the 2d array is stored in memory. For that, see: http://www-ee.eng.hawaii.edu/~tep/EE150/book/chap9/section2.1.2.html

Before allocating memory we need to know exactly how many columns (chars in each row) & rows (records)
An example might be:

char  *book;
int   records = 0;
int   maxFirstNameSize = 0;
int   maxLastNameSize = 0;
//I'm guessing phone number will always be 10 digits long

//get First & Last names
//...
//Now fill up max length variables
if ( maxFirstNameSize < strlen("CurrentFirstName") )
{
   maxFirstNameSize = strlen("CurrentFirstName");
}
if ( maxLastNameSize < strlen("CurrentLastName") )
{
   maxFirstNameSize = strlen("CurrentLastName");
}

//you can increment records everytime a new record is entered, or through a loop - however you're doing it.

book = malloc( records * (maxFirstNameSize + maxLastNameSize + 10) * sizeof (char) );

//to assign/access the array, use
book[0][0]; //location for FirstName
book[0][maxFirstNameSize]; //location for LastName
book[0][maxFirstNameSize + maxLastNameSize]; //location for phone number

//For every subsequent addition of record, use realloc()
book = realloc(book, records * (maxFirstNameSize + maxLastNameSize + 10) * sizeof (char));

> how to create a 2d dynamic array of strings called book?
Forget about making it dynamic for the moment, and go with char books[10][100]; // 10 books, up to 100 chars of info on each one Then you can do things like

strcpy( &books[0][0], "bill" );
strcpy( &books[0][20], "clinton" );
strcpy( &books[0][50], "1234567891" );

What you need are some constants for
- the length of each record
- the number of records
- the offsets of each field within the record.

Making it dynamic at the end is an easy step after you've got it working with a static array. Until then, dynamic allocation is likely to just confuse as much as anything.

thankyou both mohammad and salem for quick reply and your thoughts....much appreciated


mohammed i have a question or if anyone else can answer it...whn u declare book[0][0]; wouldnt that give compling error as it null...well its giving subscripted value is neither array or pointer!:S...do i have to define it as function???

whn u declare book[0][0]; wouldnt that give compling error as it null

If you mean initial declaration, char *book; is all you need.
For assignment/accessing memory, you can use: &(book[0]) for FirstName, &(book[0] + maxFirstNameSize) for LastName and &(book[0] + maxFirstNameSize + maxLastNameSize) for PhoneNumber.
If you meant something else, clarify.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#define max 75
int main() 
{
char  *book;
int   records = 0;
int   maxFirstNameSize = 0;
int   maxLastNameSize = 0;
char  CurrentFirstName;
char  CurrentLastName;
scanf("%c", CurrentFirstName);
scanf("%c", CurrentLastName);
//I'm guessing phone number will always be 10 digits long

//get First & Last names
//...
//Now fill up max length variables
if ( maxFirstNameSize < strlen("CurrentFirstName") )
{
   maxFirstNameSize = strlen("CurrentFirstName");
}
if ( maxLastNameSize < strlen("CurrentLastName") )
{
   maxFirstNameSize = strlen("CurrentLastName");
}


//you can increment records everytime a new record is entered, or through a loop - however you're doing it.

book = malloc( records * (maxFirstNameSize + maxLastNameSize + 10) * sizeof (char) );

//to assign/access the array, use
book[0][0]; //location for FirstName
book[0][maxFirstNameSize]; //location for LastName
book[0][maxFirstNameSize + maxLastNameSize]; //location for phone number

//For every subsequent addition of record, use realloc()
book = realloc(book, records * (maxFirstNameSize + maxLastNameSize + 10) * sizeof (char));

}

amm...sry i am confused thats why....so after reading firstname and last name..as i did..then i am goin to use str len to compare....after that i am not sure how to use loop would it be the simple for and ij loops....and also when accessing array...book[0][0]..is as u said the location..

so when i evenutally print it...it would be

printf("%s", &book[0][0]);
but i thought address was used in scanf...

i am confused on concept..itself...so sry if i am not making sense....


also search function in records would be like this

void SearchName(Contact * const srecord,char *lastname,int counter)//function defintion
{
   int y,j,count=0;
   int listofnum[20];
   	for(y=0;y<counter;y++){
		if (strcmp(srecord[y].LastName,lastname)==0){
		listofnum[count++]=y;

		}
	}
   printf("\nNumber of record found: %d ",count);
   for (j=0;j<count;j++){
   printf("\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n","First name  :",srecord[listofnum[j]].FirstName,"Last name : ",srecord[listofnum[j]].LastName,"Adress    :",srecord[listofnum
   }
}

i am pretty sure i can do this using ur main function.....eventhough this was my prof who gave us this..lol...he gave up how to do in records...an example but now asked to do this in one main function using dynamic arrays and strings...i just need
to figure out and understand the main function and make it running....since im creating a lot of int to pointer compliers errors..even with your program...thanks...for any more information....

if you please edit how to read...and loop...that would be veyr much appreciated!!...thank you...!!!!

also i included all the libraries as that was the first problem..stupid me..but still understanding &books[][] and book[][]...is out of my head and how to loop it...


also heres the link for the shortcut...for the execuatable file

<URL snipped>

thank you..so much anyone whoz helping

God-willing, I'll be free after 2 hours to think about it.

alright thank you

To understand what is going on in memory, take a look at the attached picture. char* book; simply creates a pointer which has no memory. Therefore, nothing can be assigned to it (except addresses of other memory locations). It is like a human with a head, but no body. Therefore, he cannot store food in his stomach.
How big of a stomach do we need to give him? That is determined by the amount of food we are giving him.
As far as I know (& I don't know too much), all items in the array must be of the same size. A 2d array is "simply a table" (see attached picture). However, we do not have the luxury of making one column 10 bytes wide and another 5. So what I did is used char array, for which each column is 1 byte wide. Now "Sanju" would take 5 columns. So from the 6th column we could start on the last name. "Ganju" would take another 5 columns. Then we can put the phone number starting from 11th column. I hope you are with me so far.
Doing this in C is also very simple. malloc() function is used to assign memory the first time. Now how much memory do we need? A char is 1 byte on most machines, so we need 5 bytes for first name, 5 for last name and 10 for the phone number. that's 20 bytes. book = malloc(20); That's it! Since char byte size can vary from machine to machine, it is best to use sizeof(char) . The malloc would then look like: book = malloc(20*sizeof(char)); . Once you have allocated the memory, the person can store food in his stomach. book points to the first memory byte of the allocated memory of 20 bytes. so strcpy(book, "Sanju"); would fill the first 5 columns starting from the first column. Now to put the last name, simply go strcpy( (book+5), "Ganju"); This fills up the next 5. Then, book+10 gives you the location to put the phone number.

char CurrentFirstName;
char CurrentLastName;
scanf("%c", CurrentFirstName);
scanf("%c", CurrentLastName);

char is only going to let you store one letter. make it like an array: char CurrentFirstName[50]; . Also, when you scanf() , use %s to read strings instead of only 1 letter.

To see if you were able to store the data properly, use printf(book+5); etc.

For subsequent additions of records, use realloc(book, newtotalsize); . Now that you understand the basics, try to make sense out of using maxFirstName size variables, etc... You can do it.
(HINT1: in the attached picture, you can access the name "Mohammad" by going book+23. "Ali" is located at book+31
HINT2: remember whenever you add a name which is wider than any before, you'll have to push all the names that much forward to keep them aligned).

Good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.