Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics 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.

How to store character by character in an array?

Status
Not open for further replies.

brennbar67

Member level 3
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 !! :)
 

Re: Array problem....

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

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.
 

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:
https://www.eskimo.com/~scs/C-faq/top.html
 

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!
 

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;
}
 

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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top