-2

Example:

// variable fixed
Char specialstring[PATH_MAX];
int integernumber;
int xx = 1 ; 
int yy = 1 ; 
strncopy( specialstring, "1369", ... bla); 


// here we go !! help there below please
integernumber=atoi(specialstring);
mvprintw( yy , xx , "%d" , integernumber );

Please help me in the way to convert the specialstring to an integer?

thank you

user1839724
  • 261
  • 2
  • 4
  • 6

2 Answers2

2

In your code, you have two mistakes:

1) strncopy is not the function you wants its strncpy. its man page: char *strncpy(char *restrict s1, const char *restrict s2, size_t n); Here s1 is destination string and s2 is source string , n is number of chars you wants to copy from source.

So correct:

strncopy( specialstring, "1369", ... bla); 
     ^                            ^ should be `n` num of chars you wants to 
    strncpy                         copy in `specialstring`

into

 strncpy( specialstring, "1369", 4); 

2) In declaration of specialstring, Char is wrong you should write small c

Char specialstring[PATH_MAX];
^ small letter

char  specialstring[PATH_MAX];

3) atoi() is correct function you got to convert a string into int, if you wants to convert without atoi you can use sscanf() function like:

sscanf(specialstring,"%d", &integernumber);

View this: Working Code

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
-1

You can use this to convert string to int without using atoi

int Convert(char * str)
{
    int result =0;
int len=strlen(str);
for(int i=0,j=len-1;i<len;i++,j--)
    {
    result += ((int)str[i] - 48)*pow(10,j);
    }

   return result;
}
singh
  • 439
  • 1
  • 4
  • 20
  • -1. This assumes ASCII. The implementation of `atoi` on a given platform may take into consideration the fact that it uses another form of storing characters, which is why it should be used instead. Also, `pow` operates on floating-point values, which is simply unnecessary in this case. – Daniel Kamil Kozar Mar 30 '13 at 11:10
  • Sorry...but can you explain more...and isn't this a valid code for converting string to integer?? – singh Mar 30 '13 at 11:12
  • It is valid if the platform where that code is used uses ASCII and if you, as a programmer, don't mind having unnecessary operations in your code. – Daniel Kamil Kozar Mar 30 '13 at 11:14
  • Not to mention that it does not support signed numbers, which are properly converted by `atoi`. – Daniel Kamil Kozar Mar 30 '13 at 11:21