Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

How to store character by character in an array?

Status
Not open for further replies.

brennbar67

Member level 3
Joined
Jul 26, 2004
Messages
54
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
580
Array problem....

Dear all,

Does anyone know how to stor character by character into a array?
That mean, in beginning the contain inside array is empty, when a user keying a number/alphabet, i need to stor the number/alphabet into the array.
Here is my program,but when i complie,it give me some warning...like this <assign far pointer to near pointer,bank value ignored>, why?


unsigned char *phonebook[20];
unsigned int phone_size=0;

void main()
{....
....
phonebook[phone_size]= "9"; //assume user key in number 9
phone_size++;
....
....
display();

}

void display(void)
{ int i;
for(i=0;i<10;i++)
{
phonebook;
}
}


Does anyone can point out my problem,pls??thanks !! :)
 

zkai2000

Member level 5
Joined
Jul 25, 2004
Messages
94
Helped
0
Reputation
0
Reaction score
2
Trophy points
1,286
Activity points
907
Re: Array problem....

do u really need pointers for that? y not use "char input []" ?
u can try dynamic memory if u need pointers
 

cherrytart

Advanced Member level 4
Joined
Feb 26, 2002
Messages
118
Helped
5
Reputation
10
Reaction score
1
Trophy points
1,298
Location
oklahoma
Activity points
1,037
Array problem....

maybe you should change

phonebook[phone_size]= "9"

phonebook[phone_size]= '9'

So that it is assigned a character and not a string.

Constant strings are often collected into a single location and this might require a far pointer.
 

echo47

Advanced Member level 5
Joined
Apr 7, 2002
Messages
3,942
Helped
636
Reputation
1,270
Reaction score
88
Trophy points
1,328
Location
USA
Activity points
33,183
Array problem....

Please post a complete program. That fragment doesn't compile and doesn't show us what you are trying to do.

If this is suppose to be a C program,

.... is a syntax error.

phonebook has no effect, so display() does nothing.

You didn't declare display() before calling it.

main() returns int, not void. If you got that from a Schildt book, throw it out!

Lots of good C programming advice here:
http://www.eskimo.com/~scs/C-faq/top.html
 

zkai2000

Member level 5
Joined
Jul 25, 2004
Messages
94
Helped
0
Reputation
0
Reaction score
2
Trophy points
1,286
Activity points
907
Re: Array problem....

hi,
i hope this helps:

Code:
#include<iostream.h>
#include<stdlib.h>
int main()
{
	char input [100];
	int i,n;
	long*l;
	cout << "How many numbers do you want to key in?";
	cin.getline (input,100); i=atoi(input);
	l=new long[i];
	if (l == NULL) exit (1);
	for (n=0;n<i;n++)
	{
		cout<<"Enter number: ";
		cin.getline (input,100);l[n]=atoi(input);
	}
	cout<<"You have enterd: ";
	for (n=0;n<i;n++)
		cout << l[n]<<", ";
		delete[] l;
		return 0;
}
cheers!
 

btbass

Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,897
Helped
438
Reputation
880
Reaction score
287
Trophy points
1,363
Location
Oberon
Activity points
12,861
Re: Array problem....

Your declaration 'char *phonebook[20];' declares an array of 20 pointers to char's. This is commonly called a ragged array.
To populate the array, you need a buffer and something like 'gets(buffer)' to get the input. 'gets()' reads a string up to, but not includung the return.You then need to allocate memory for the string and copy it to your array.
This is not so good, how big should you make the buffer? Some maliscious soul will try to overflow it, and get into your phonebook. Then he can phone up that cute chick with the nice ass that you had your eyes on!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

int main(int argc, char* argv[])
{
char *phonebook[20];
char buffer[40];

gets(buffer);
phonebook[0] = (char *) malloc(strlen(buffer)+1);
sprintf(phonebook[0], buffer);
printf(phonebook[0]);
getch();
return 0;
}
 

Hughes

Advanced Member level 3
Joined
Jun 10, 2003
Messages
717
Helped
113
Reputation
226
Reaction score
25
Trophy points
1,298
Activity points
5,986
Re: Array problem....

Code:
unsigned char phonebook[20];
unsigned int phone_size=0;

void main()
{....
  ....
phonebook[phone_size]= '9';          //assume user key in number 9
phone_size++;
 ....
 ....

 }
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top