Thread: char to integer

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    19

    char to integer

    I have a little problem with my code I have a pointer to integer variable and I want to assign it a char value.
    Can I do that???

    Code:
    int *value, total;
    char **p;
    
    for (i=0; i< total; i++)
    value = p[i];
    
    printf(" Value %d\n", value);
    This code give me an error value = p[i].... Operands of = have incompatible types 'int *' and 'char *'

    Help please!!!
    Last edited by Ken Fitlike; 08-21-2007 at 04:01 PM. Reason: fixed [code][/code] tags

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It's [code] code here [/code].

    Do you want to convert a char value like '3' and wish to convert it to an integer like 3? If so, just subtract '0'.
    Code:
    int char_to_number(char c) {
        return c - '0';  /* assumes c is a digit; perhaps make use of isdigit() from <ctype.h> */
    }
    What are you trying to do?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Really hard to know what you are trying to do.

    1. None of your variable are initialized, except i
    2. Your loop is totally useless (even in the case total would be initialized). Use an if statement

    You really playing with undefined behaviour.

    I have a pointer to integer variable and I want to assign it a char value
    There you go
    Code:
    int *p = (int *) 'z';

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    19

    Char as integer

    I didn't want to write all the code so that's why I abbreviate it in my first post.

    Code:
    typedef struct include_table{
    		char	*ctrl_id;
    		int		*ctrl_value;
    	}FINCLUDE;
    
    int fill_inc_struct(char **a_p_ibuf, FINCLUDE *f_table, int total)
    {
    	int 		i=0, x=0;
    
    	for (i=0; i<total; i++)
    	{
    		if (!isdigit(*a_p_ibuf[i]))
    		{
    			f_table[x].ctrl_id = *(a_p_ibuf+i);
    			printf(" %s \n",(f_table[x].ctrl_id));
    		}
    		else
    		{
    			f_table[x].ctrl_value=(int*)(a_p_ibuf[i]);
    			printf(" %d  \n",(f_table[x].ctrl_value));
    			x++;
    		}
    		
    	}
    	
    	return 1;
    }
    What I am trying to do is fill the structure with the data from my **p variable that points to the begining of each of the strings ... TITLE 101 CODE 102.... (**p point to T and 1 and C and 2....). My structure should have at the end
    ctrl_id = TITTLE
    ctrl_value = 101 and so on..

    I have no problem with the char pointer, cause it prints what it should("TITLE, CODE), but the int pointer prints 12343039... 1243066.. ans it should print 101... 102.

    Could somebody tell me what the problem is.

    thanks

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Use a char * instead of an int * for your ctrl_value variable. You are poiting to a char, not an int, and it makes no sense to use an int * to point on characters (in 99.9&#37; of the case).

    Code:
    typedef struct include_table
    {
    	char	*ctrl_id;
    	char	*ctrl_value;
    }FINCLUDE;
    [edit] And don't forget to switch the format code for %s in your printf instruction after that [/edit]
    Last edited by foxman; 08-22-2007 at 09:02 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  2. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. checking if a char * is an integer
    By gregulator in forum C Programming
    Replies: 5
    Last Post: 04-16-2004, 09:12 AM
  5. AnsiString versus char *
    By Zahl in forum C++ Programming
    Replies: 35
    Last Post: 10-16-2002, 08:38 PM