I have a char pointer here,

char* buffer = (char*)malloc(sizeof(char*));
*buffer = 1;
buffer++;
*buffer = 2;

so by doing this, i actually have the pointer pointing to "1" and "2"
what i actually want is the number, 12.
anyone knows how can i get the char pointer's data "1" and "2" converted into the number 12 in an integer variable?

Seems like you are overcomplicating things.. Also, what's the sizeof( char *)?

char buffer[3];
memset( buffer, '\0', 3 );
strcpy( buffer, "12" );
int myInt;
sscanf ( buffer, "%d", &myInt );

thanks for replying to the post~!
but sad to say, you haven't answered my question yet.
The problem here is that i have a char pointer pointing to a string of char numbers.
'1' is listed 1st on the row, '2' is listed 2nd.
so i gotta increment the address to access the 2nd number, ie buffer++
and my question is how can i get the char numbers out, concatenate them so that they become '12', and then store them in an int variable.
I'm not trying to overcomplicate things because this is what is required in the code itself.

I am still concerned with:
char* buffer = (char*)malloc(sizeof(char*));

as size of char* is always 4. Is that what you (or the previous programmer) wanted?

In any case:

#include <stdlib.h>

int atoi(const char *nptr);

as size of char* is always 4.

Well, except when it isn't. :p

Dave, please elaborate..

The size of a pointer may commonly be 4 these days. It is unfortunate, too, that this happens to be the size of an integer on such systems. Unfortunate because this leads to an assumption that sizeof(int) == sizeof(void*).

I can still use Borland to create DOS code for which sizeof(char*) is 2. I've developed for systems where it could be 3. In the future it could be 8.

http://cboard.cprogramming.com/showthread.php?p=468408&highlight=linux#post468408

[edit]Pointers to different types do not even need to be the same size or format. (Before you ask, such critters have already existed.)

(Regardless, his code is invalid, right?)

ok...i'll take out the sizeof then, and hard code it with a fixed size...

char* buffer = (char*)malloc(256);
*buffer = 1;
buffer++;
*buffer = 2;

so now...i have a char pointer pointing to string '1','2'
i need it to be the number 12 in an integer variable
how to go about doing this?

In any case:

#include <stdlib.h>

int atoi(const char *nptr);

Or sscanf, or strtol. But you'll need to use characters and null terminate the string first, and point to the start of the string.

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

int main()
{
   int value = 0;
   char *buffer = malloc(256);
   if ( buffer )
   {
      char *ptr = buffer;
      *ptr++ = '1';
      *ptr++ = '2';
      *ptr   = 0;
      // atoi, strtol, sscanf
      value = atoi(buffer);
      free(buffer);
   }
   printf("value = %d\n", value);
   return 0;
}

/* my output
value = 12
*/

that solved my problems~!!
thanks a million!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.