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

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.

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

Aia,Could you help with the sprintf to write into a integer variable?

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 :)

An example of sprintf:

int i = 52300;
char s[30];
sprintf(s, "%d", i); // s now contains the value 52300

>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?

sscanf or http://www.daniweb.com/code/snippet441.html strtoul?

#include <stdio.h>

int main()
{
    const char representation[] = "1234";
    int value;
    if ( sscanf(representation, "%d", &value) == 1 )
    {
        printf("value = %d\n", value);
    }
    return 0;
}

/* my output
value = 1234
*/
commented: sscanf is just such a useful function!! +7

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.

commented: no, it's not. -2

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;
}

Good solution!!!

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.