Thread: int array to char array

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    88

    int array to char array

    OK say I have an integer array of 15.
    and a char array of 15.
    What function could I use to transfer the integer array's values to the char array?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How about the assignment operator?
    Code:
    for( x = 0; x < 15; x++ )
        carray[x] = iarray[x];
    Naturally, any value that falls outside the range of type char will be truncated some how. Your compiler will likely warn you of it. If you want more than that, you'll have to be specific as to how you have in mind to transform integers to characters.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    88
    Thanks, Quzah

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    88
    ok I have run into a problem

    This portion of code reads in from a file character by character, starting with a number.
    I want to send the whole number to value in struct Lexeme.
    But I still need to use value for strings, as well.
    I hvae attempted many things, all to no avail. I am sure the problem is extremely obvious to you all, but I have been staring at this part of code for a couple of hours now.
    Can someone please tell me what I'm doing wrong?

    Code:
    typedef struct
    {
    	char type[15];
    	char value[15];
    }Lexeme;
    
    Lexeme *pointer;
    
    int LexNumber()
    {
    	char ch, temp;
    	ch = readchar();
    	temp = ch;////////I know the problem is with this line and
    	while(isdigit(ch) != 0)
    	{
    		ch = readchar();
                   strcat(temp, ch);////////////////////this line
    	}
    
    	pushback(ch);
    	return newLexeme(NUMBER);
    }
    
    int readchar()
    {
    	char ch;
    	ch = fgetc(readfile);
    	if(ch == EOF)
    	{
    		return newLexeme(ENDOFINPUT);///displays too soon
    	}
    	return ch;
    }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm assuming this is a text file? And you want it a line at a time? Sounds like a good reason to use fgets. Read the line into a buffer, then walk through the buffer doing whatever it is you need to do.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    88
    actually I need to read it in one character at a time. I would love to use fgets but when lexemenumber is called, I need to store the string "number" in a struct, as well as the value of that number in that struct. Since there is more on a line than just numbers, I need to read it in one char at a time and once I hit a non-number I quit.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    88
    okay, I figured it out, a break to watch DUBYA, must have made me smarter.
    Funny... I didn't see that coming.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM