Thread: How do I properly put char values into an array?

  1. #1
    Registered User
    Join Date
    May 2014
    Posts
    10

    Question How do I properly put char values into an array?

    I'm trying to fill the array "g" with letters from the array "letras" given a certain condition. Everything is working fine, except I couldn't do it... Strange characters appear when I run the code. What am i doing wrong?

    Note: This is a part of a function. "vetor" is a parameter that was passed to this function.

    Code:
            int i;
    	int j = 0;
    	char g[20];
    	char letras[5] = {'a', 'b', 'c', 'd', 'n'};
    	
    	while(j < g)
    	{
    		for(i = 0; i < 80; i = i + 4)
    		{
    			if(vetor[i] == 1 && vetor[i + 1] == 0 && vetor[i + 2] == 0 && vetor[i + 3] == 0)
    			{
    				letras[0] = g[j];
    				printf("%c\n", g[j]);
    				j++;
    			}
    			else if(vetor[i] == 0 && vetor[i + 1] == 1 && vetor[i + 2] == 0 && vetor[i + 3] == 0)
    			{
    				letras[1] = g[j];
    				printf("%c\n", g[j]);
    				j++;
    			}
    			else if(vetor[i] == 0 && vetor[i + 1] == 0 && vetor[i + 2] == 1 && vetor[i + 3] == 0)
    			{
    				letras[2] = g[j];
    				printf("%c\n", g[j]);
    				j++;
    			}
    			else if(vetor[i] == 0 && vetor[i + 1] == 0 && vetor[i + 2] == 0 && vetor[i + 3] == 1)
    			{
    				letras[3] = g[j];
    				printf("%c\n", g[j]);
    				j++;
    			}
    			else
    			{
    				letras[4] = g[j];
    				printf("%c\n", g[j]);
    				j++;
    			}
    		}
    	}

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by eduarda2 View Post
    I'm trying to fill the array "g" with letters from the array "letras" given a certain condition. Everything is working fine, except I couldn't do it... Strange characters appear when I run the code. What am i doing wrong?

    Note: This is a part of a function. "vetor" is a parameter that was passed to this function.

    Code:
        int j = 0;
        char g[20];
        while(j < g)
        {
            for(i = 0; i < 80; i = i + 4)
    Pointer comparison is undefined.

  3. #3
    Registered User
    Join Date
    May 2014
    Posts
    10
    I just figured it out! i did:
    ex:
    Code:
    g[j] = letras[0];
    just put the "g" first. haha
    But thanks anyway!

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Aslaville View Post
    Pointer comparison is undefined.
    I think you mean to say, comparing a pointer with an integer is undefined? (probably implementation defined technically). Comparing pointers with pointers should be fine. E.g. suppose you want to specify a string by a pair of pointers (no sentinel required) then you could implement string functions something like this

    Code:
    int countletter(char letter, const char *str, const char *strend)
    {
        int p = 0;
        for (const char *s = str; s < strend; ++s)
            if (*s == letter) ++p;
        return p;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do i assign values to ponter char array????
    By De shaa in forum C Programming
    Replies: 2
    Last Post: 08-11-2013, 04:01 AM
  2. scanning values from a char array
    By The Doctor in forum C Programming
    Replies: 2
    Last Post: 09-04-2012, 03:04 AM
  3. Indexing an Array, Based on Char Values from another.
    By Venatio in forum C Programming
    Replies: 3
    Last Post: 07-20-2012, 03:54 PM
  4. fill unsigned char array with hex values
    By luigi40 in forum C Programming
    Replies: 1
    Last Post: 06-25-2005, 05:56 AM
  5. Comparing char values in an array using 'if'
    By Judy_528 in forum C++ Programming
    Replies: 18
    Last Post: 04-25-2002, 07:52 AM

Tags for this Thread