Hi All,

Currently i'm using sprintf to copy from pointer variable to a character array.

sprintf(length,"%.*s",(int)len,part1);

where part1 is a pointer to a character array..
length is a characteter array too.

Is there a way i can do the above thingy to copy the part1 to a integer variable?

Thanks in advance

Edited 3 Years Ago by pritaeas: fixed formatting

So, you want to convert a character string to an integer variable, try atoi !

example:

char num[] = "5269";
int i;
i = atoi(num); // integer variable i now holds the number 5269

Thanks! atoi works...
part1 consists of a number value.

But wat if i use atoi on a character array variable which has alpha numeric value, will it core dump?

sanushks> Thanks! atoi works...
Do you know what happens if atoi fails to convert?
Your first choice of using sprintf was superior, since error checking is better with it.

Then a zero value is returned, or am I wrong?

You're wrong. If you pass a NULL pointer to atoi, your program will most lightly crash. If you pass a string such as "12Q34" then it will return 12, as it stops as soon as it reaches the first non-digit character.

You're wrong. If you pass a NULL pointer to atoi, your program will most lightly crash. If you pass a string such as "12Q34" then it will return 12, as it stops as soon as it reaches the first non-digit character.

Oh, he means 'fails' in that way, then I agree :)

>Do you know what happens if atoi fails to convert?
Then a zero value is returned, or am I wrong?

atoi either returns 0 or invokes undefined behavior. But you can't stop the undefined behavior without carefully validating the string before calling atoi. You also can't tell if the 0 is an error or a legitimate conversion without naively validating the string before calling atoi. If you have to validate the string in every case to use a conversion function in a robust way, the function should be avoided.

So can sprintf be used to copy the array contents to a integer variable? Or is there any other function i can use to accomplish this?

Hi All,

Currently i'm using sprintf to copy from pointer variable to a character array.

sprintf(length,"%.*s",(int)len,part1);

where part1 is a pointer to a character array..
length is a characteter array too.

Is there a way i can do the above thingy to copy the part1 to a integer variable?

Thanks in advance

Yes, you can copy content of part1 string to length string variable. Your code is correct.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Comments
no, it's not.

So can sprintf be used to copy the array contents to a integer variable? Or is there any other function i can use to accomplish this?

i prefer to use "strtol()" to allow error checking that doesnt confuse failed conversion with a valid zero (0) conversion.

#include <stdio.h>
#include <string.h>
#include <errno.h>

int main (void)
{
   char myString[32];
   char *remStr = myString;
   long value;
   
   while(1)
   {
      printf("\n\nenter an integer string: ");
      fflush(stdout);
      
      fgets(myString,sizeof(myString),stdin);
      myString[strcspn(myString,"\n")]='\0';
      
      errno=0;
      value = strtol(myString,&remStr,0);
      
      if (errno)
         perror("Error, integer conversion");
      
      else if (strncmp(myString,"Q",1)==0 ||
               strncmp(myString,"q",1)==0    )
      {
         printf("quit requested.\n");
         break;
      }
      
      else if (remStr == myString)
         printf("Invalid, no conversion\n");

      else
      {   
         printf("   integer value = %ld\n",value);
         
         if (*remStr != '\0')
            printf("   string remainder: \'%s\'\n",remStr);
      }        
   }
   return 0;
}
This question has already been answered. Start a new discussion instead.