I'm trying to convert each letter in a string to it's ASCII number. Using
int letter = (atoi(ptext[i]));
gives me this error:
error: incompatible integer to pointer conversion
passing 'char' to parameter of type 'const char *'; take the
address with & [-Werror]
int letter = (atoi(ptext[i]));
^~~~~~~~
&
/usr/include/stdlib.h:148:32: note: passing argument to parameter
'__nptr' here
extern int atoi (__const char *__nptr)
Here is some of the rest of my code that may be relevant:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, string argv[])
{
printf("Give a string to cipher:\n");
string ptext = GetString();
int i = 0;
if (isupper(ptext[i]))
{
int letter = (atoi(ptext[i]));
}
}
What am I doing wrong, and how do I fix this so I can convert a string into ASCII values?
Note: cs50.h
lets me use "string
" instead of "char*
" in main.
atoi()
expects a string. You only want the char code of the character... which is the character itself, since in C, the char
is a normal integer type just like every other integer, and a string is an array of char
s which hold the character codes themselves.
Consequently,
int letter = ptext[i];
would do the job.