I am trying get number ASCII for letter in char variable. My code so far is:
int main (int argc,char* argv[]) {
char c="A"; //here error
char b="'"; //here error
char d="z"; //and here error
printf("%d\n %d\n %d\n",c,b,d);
}
But I get the following errors:
analizer.c: In function ‘main’:
analizer.c:13:8: warning: initialization makes integer from pointer without a cast [enabled by default]
analizer.c:14:8: warning: initialization makes integer from pointer without a cast [enabled by default]
analizer.c:15:8: warning: initialization makes integer from pointer without a cast [enabled by default]
"A" is a string literal (an array of characters). 'A' is a character constant (an integer). What you want should be latter.
#include <stdio.h>
int main (int argc,char* argv[]) {
char c='A';
char b='\''; // use escape sequence
char d='z';
printf("%d\n %d\n %d\n",c,b,d);
}
The compiler will allow you to extract elements of the arrays (string literals) like below, but using character constants should be better in this case.
#include <stdio.h>
int main (int argc,char* argv[]) {
char c="A"[0]; // a typical way to deal with arrays
char b=*"'"; // dereferencing of a pointer converted from an array
char d=0["z"]; // a tricky way: this is equivalent to *((0) + ("z"))
printf("%d\n %d\n %d\n",c,b,d);
}
Basically you point to a string literal when you do this (which is invalid):
char c="A";
You need to write a single char to the variables:
char c='A';
char b='\'';
char b='z';
In C strings are just an array of characters which terminate with a \0. So if you want to use only one character, declare a char variable and use the character single quote. If you want to use a string, declare char array and use doublequote as
char a='A' // just one character
char b[20]="Some_Text"// multiple characters(a string)
In the first case, a contains the integer value of 'A' but in the second, b contains the base address of the string it points to. Each character in array b[] must be accessed with indexes as b[0] b[1] etc.
You can use strings to represent single characters as
char a[]="A" // a string of size 1
and then you can print it's integer equivalent as
printf("%d",a[0]); //0th element of the string
or
use the single quote method as described in other answers.
Related
I created a string in the main body and I want a function to access it. In order to access it I have to use the name of the string, not the pointer to it:
#include <stdio.h>
void test(char * s,char * b){
printf("%s\n",s);
//printf("%s\n",*s); this crashes it
printf("%c\n",*b);
}
int main(){
char s[]="string";
char b='b';
char * pointerb=&b;
char * pointerstring=&s;
test(pointerstring,pointerb);
}
Why is line 5 correct and not line 6?
printf("%s\n",*s);
%s is a format specifier for string. it expects pointer to array of char, but you are passing argument type is char. this is the reason for segmentation fault.
another side note, your has warning for this line
char * pointerstring=&s;
need to change to below.
char * pointerstring=s;
char* pointerstring now points to first char of string s[] which is nothing but 's'
Here is stack visualization of how pointer point to the string when program reaches at line char * pointerstring=s;
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 chars which hold the character codes themselves.
Consequently,
int letter = ptext[i];
would do the job.
You do not need to convert characters to a number. It is a matter of interpretation of your data.
Charater 'A' can be thought of 0x41 or 65 so this is perfectly fine:
int number = 'A';
and value on variable number is 0x41/65 or 1000001b depending how you want to present it/treat it.
As for interpretation : 0xFF may be treated as 255 if you present it as unsigned value or even as -1 when treated as signed and kept in 8 bits.
So your question:
can convert a string into ASCII values?
is kind of wrong - all characters of the string are already ascii values - it is just a matter of how you treat/print/interpret/present them.
int letter = (atoi(ptext[i]));
atoi() convert string to integer not character.
To store ascii value of character into integer variable Do direct assignment of character to integer variable.
int letter = ptext[i];
i want to get the first character of a string (char[]) in C.
unsigned int N;
unsigned int F;
unsigned int M;
char C;
int main (int argc, char *argv[]){
if (argc!=5){
printf("Invalid number of arguments! (5 expected)\n");
exit(-1);
}
N = atoi(argv [1]);
F = atoi(argv [2]);
M = atoi(argv [3]);
C = (char) argv[4]; //this way gives a wrong char value to variable C
The code above gives me the warning: cast to pointer from integer of different size.
EDIT: as pointed in comments, argv is char *[], not char[].
There are two main ways to do this. The first is to simply dereference the pointer.
C = *argv[4];
You can also do it via array subscript, which implicitly adds an offset to the pointer before dereferencing it.
Be sure to check whether it's null first, and so on. In general, you need to be careful when dealing with pointers.
argv[4] is a char array. You need to dereference that array to obtain a single element from it
C = *(argv[4]);
#include <stdio.h>
int main(void)
{
int i,j,k;
char st;
printf("enter string\n");
scanf("%s", st);
printf("the entered string is %s\n", st);
}
Compiling above program gives me a warning:
warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat]
palindrom.c:8:1: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat]
What am I doing wrong here?
This is what happens when I run it:
$ ./a.out
enter string
kiaaa
the entered string is (null)
Edit:
Here is another version of the code (made char st; into char *st):
#include <stdio.h>
int main(void)
{
int i,j,k;
char *st;
printf("enter string\n");
scanf("%s", st);
printf("the entered string is %s\n", st);
}
However, it behaves the same on runtime.
char st is a single character. Judging by the rest of your code, you probably intended to declare an array of characters:
char st[80];
scanf needs a pointer to char* to indicate you are scanning a string.
You are supplying a character that's allocated on the stack.
You either want to use a getchar() or make st a char array.
You have a type mis-match.
scanfis not type safe, You need to provide proper type. scanf enables you to get data from the input and you need to tell it what is the type of the data you want it to read. You ask it to read a string by specifying %s by provide it with a character variable.
You need an array:
#define MAX_LENGTH 256
char st[MAX_LENGTH];
As #Jerry rightly points out, You can just simple avoid all the hassle by using:
getline() instead of using scanf
st is type of char
&st is type of char *
Take care of the difference.
BTW, only one char cannot be used to store a string. Use char[] array.
As others have mentioned you want to create an array:
Change char st; to char st[10]; or whatever size array you want.
With the above change st is an array that has 10 individual elements that can hold a single char value.
char st is a character i.e it will store only one character try it like this
main()
char st [100];
scanf("%s",st);
just change these three lines and it will work it will except only single word strings
here you have declared the function as an int that is int main() keep it as main() and it will work
Use char *st; or an array like char st[50];.
When you complete the usage of a char pointer, you should deallocate the memory used by the pointer. This can be done using free(st); function.
EDIT : As you are printing string and if you are using pointer, you can do:
printf("the entered string is %s\n",st);
printf("the entered string is %s\n",*st); // This will work in both cases, if you use char *st or char st[50]
I was just going through certain interview questions. Got this structure related issue, I m not understanding what is happening wit the output, Please if some could explain the reason.
When is use a character pointer in the structure like
#include <stdio.h>
struct name {
char *array;
}variable;
int main( int argc, char * argv[] ){
variable.array="hello";
printf( "%s\n", variable.array );
}
The output is hello printed, but when change the structure variable to
struct name {
char array[10];
}variable;
The compiler throws an error "incompatible types in assignment" at
variable.array="hello";
I am really confused as to where i am missing the point. Why does it show the error like assignment problem?? Please correct me
Thank you
You can only initialize array like that at the time of declaration only,
else you need to use
strcpy(variable.array,"hello");
you can't even do like that with a simple char array
char a[10];
a="hello";
the complier will tell :
incompatible types when assigning to type ‘char[10]’ from type ‘char *’
because "hello" is a string literal that is being held by a pointer which can't be assigned like this to an array.
In C the term "hello" means "a pointer to a string that has the chars 'hello\0' in it".
So when you assign array="hello" in the first case you are assigning the pointer to the string into a pointer variable. Which is right.
In the second case you try to assign a pointer to an array which is wrong!
you can't assign value to an array.
It raises an error because an array of char and a pointer to char are different things.
An array is not a pointer. A pointer is not an array. Sometimes a pointer might point to an array. But even when a pointer points to an array, it's not an array.
Your compiler gives an address to variable.array at compile time. It also gives an address to the string literal "hello". They're not the same address. The string literal and the array (or "the buffer") are in different places; logically, the string has to be copied into the buffer.
#include <stdio.h>
#include <string.h>
struct name {
char array[10];
}variable;
int main( void ){
strcpy(variable.array, "hello");
printf( "%s\n", variable.array );
return 0;
}
You might find the C FAQ useful.
The first thing is array is a constant pointer to the first element in the
array,we can not change its address location once we declared..like
int a[3]; is equivalent to int *const a;
so we cant change the array address location
int a[3]={1,2,3};
a++;//its an error
int b[3];
b=a;//its also an error
but int *c;
c=a;//Not an error because c is a pointer to integer not constant pointer to integer
similar to
char str[10]="hello";
if i change like this
str="world";//its also an error
Pointers and arrays are always not equal, in some cases only like dereferencing
char a[10]="hello";
for(i=0;a[i]!='\0';i++)
printf("%c",a[i]);
for(i=0;a[i]!='\0';i++)
printf("%c",*(a+i));