How can i add Add two int chars together into one char array or string like :
char *s;
int a = 'A';
int b = 'B';
s = a + b;
the terminal givs me :
incompatible integer to pointer conversion assigning to 'char *' from
'int'
Have a look at sprintf to print the values to a string.
char my_cstring[32] = "";
char a = 'A';
char b = 'B';
sprintf(my_cstring, "%c%c", a, b);
// output: "AB"
Related
int main() {
int n, b, i, j;
char *thousand;
//char a;
char symbol [] = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};
printf("Enter an integer between 0 and 10000: ");
scanf("%d",&n);
if( n >= 1000)
{
b = n / 1000;
for(i = 0; i < b; i++){
for (j = 0; j < i; j++)
{
thousand = symbol [6];
}
}
}
printf("%s",thousand);
return 0;
}
Here is the error that I am getting:
incompatible integer to pointer conversion assigning to 'char *' from 'char'; take the address with & [-Wint-conversion]
thousand is char *. symbol[6] is char. Thus the error message
incompatible integer to pointer conversion assigning to 'char *' from 'char'
You should change char *thousand to char thousand or thousand = symbol [6]; to thousand = symbol + 6 depending on what you want. Probably the first one.
The variable thousand is declared as:
char *thousand;
And in this line:
thousand = symbol [6];
thousand, which is a char * is assigned from symbol[6], which
is declared as:
char symbol []
Thus the error:
...conversion assigning to 'char *' from 'char'...
as symbol[6] is a char, not a char *.
Here you declared thousand as character pointer , which means it can store address of a character variable not a character itself. But you are trying to assign a character in thousand. Always remember that pointer stores address.
How to concatenate two characters and store the result in a variable?
For example, I have 3 characters 'a', 'b' and 'c' and I need to join them together and store them in an array and later use this array as a string:
#include <stdio.h>
#include <string.h>
char n[10];
main ()
{
// now I want insert them in an array
//ex: n = { a + b + c }
}
There are many ways, the following 2 methods have exactly the same results: (using your code as a starting point.)
#include <stdio.h>
#include <string.h>
int main(void)
{
//Given the following
char a = 'a';
char b = 'b';
char c = 'c';
// assignment by initializer list at time of creation
char n1[10] = {a,b,c};
//usage of a string function
char n2[10] = {0}; //initialize array
sprintf(n2, "%c%c%c", a, b, c);
return 0;
}
Both result in a null terminated char arrays, or C strings.
Simply:
char a = 'a', b = 'b', c = 'c';
char str[4];
str[0] = a;
str[1] = b;
str[2] = c;
str[3] = '\0';
Or, if you want str to be stored on the heap (e.g. if you plan on returning it from a function):
char *str = malloc(4);
str[0] = a;
...
Any introductory book on C should cover this.
An assignment similar to that can only be done when the char array is declared using array initialiation with a brace-enclosed list:
char a = 'a', b = 'b', c = 'c';
char n[10] = {a, b, c};
After the declaration you can't do it like this because a char array is not a modifiable lvalue:
n = {a, b, c}; //error
To insert characters in an array that has been previously initialized, you need to either insert them one by one as exemplified in another answer, or use some library function like sprintf.
sprintf(n, "%c%c%c", a, b, c);
In both of my examples the char array will be null terminated by the compiler so you can use it as a string, if you assign the characters one by one, make sure to place a null terminator at the end ('\0'), only then will you have a propper string.
I have two unsigned int pointers with 32 bit and I want do an XOR operation between these unsigned pointers.
char* a = "01110011011100100110111101000011";
char* b = "10111001100011001010010110111101";
unsigned* au = (unsigned*) a;
unsigned* bu = (unsigned*) b;
unsigned* cu = a ^ b;
Error is:
invalid operands to binary ^ (have ‘unsigned int *’ and ‘unsigned int *’)
You have strings, not unsigned integers. You'll need to convert them to unsigned integers before you can do bitwise operations on them:
char* a = "01110011011100100110111101000011";
char* b = "10111001100011001010010110111101";
unsigned au = strtoul(a, 0, 2);
unsigned bu = strtoul(b, 0, 2);
unsigned cu = a ^ b;
Your code needs to work a little harder to pull that off.
Iterate over the strings.
Pull the numbers from the digits.
Perform an XOR between the numbers.
Convert the number to a digit.
Add the resultant digit to the output.
char str1[] = "01110011011100100110111101000011";
char str2[] = "10111001100011001010010110111101";
char* p1 = str1;
char* p2 = str2;
for ( ; *p1 != '\0' && *p2 != '\0'; ++p1, ++p2 )
{
unsigned int n1 = *p1 - '0';
unsigned int n2 = *p2 - '0';
unsigned int n = n1 ^ n2;
char c = n + '0';
// Now you can change either a or be to contain the output.
*p1 = c;
}
// At this point, str1 should contain a string that looks like you performed an XOR between str1 and str2.
I'm trying to accomplish the following:
given the function:
int f(void *p)
take the first byte of p and extract it as a character. Then based on what that character is determine which additional bytes to extract as short ints. For example if I have a certain character c then extract byte 4 and 7 as short ints and store them in separate variables; if I have a certain different character d extract bytes 3, 4, 5 and store them as separate variables. With this information execute some other irrelevant code. I've been struggling with this for hours.
I tried the following:
int f(void *p) {
char *first = &p;
short int *third = p + 2;
short int *fifth = p + 4;
short int *seventh = p + 6;
printf("%s %s %s, %s", first, third, fifth , seventh);
}
int main(int argc, char const *argv[]) {
char test[] = "*5431234567";
f(test);
}
My result:
▒▒ϲ -1295010922 -1295010920, -1295010918
Expected:
* 4 1 3
After a while I realized I was attempting to de-reference a void pointer which does not work. So I tried casting:
char* c = (char*) p;
char first = (char) (*c)
printf("%s", first);
This gave me a seg fault.
I tried similar things with short int casting to no avail. If this is a somewhat noobish question I apologize. I'm new to C and the whole concept of pointers and references is new to me. My first language was Java which is much more forgiving.
Your input is a string, and your expected output are individual characters within the string. There is nothing short int-like about this problem.
char *s = p;
printf("%c %c %c, %c", s[0], s[2], s[4] , s[6]);
If you need to store those values into short int variables, then you do not need pointers.
short int x = s[2] - '0';
short int y = s[4] - '0';
short int z = s[6] - '0';
printf("%c %hd %hd %hd\n", s[0], x, y, z);
The characters that represent the decimal digits are guaranteed to be contiguous, so you can compute the ordinal value by calculating the offset from '0'.
char *first = &p;
Get rid of the &. You don't want the address of the p variable, you want the address stored in it, which is simply p.
char *first = p;
short int *third = p + 2;
short int *fifth = p + 4;
short int *seventh = p + 6;
printf("%s %s %s, %s", first, third, fifth, seventh);
There's no reason to be using short pointers. The elements of a string are chars, not shorts. Even when they're digits, they're still chars.
Leave them as chars, switch to %c to print them, and make sure to dereference the pointers when you do.
char *third = p + 2;
char *fifth = p + 4;
char *seventh = p + 6;
printf("%c %c %c %c\n", *first, *third, *fifth, *seventh);
AFunc changes what was sent to it, and the printf() outputs the changes:
void AFunc ( char *myStr, int *myNum )
{
*myStr = 's';
*myNum = 9;
}
int main ( int argc, char *argv[] )
{
char someString = 'm';
int n = 6;
AFunc(&someString, &n);
printf("%c" "%d", someString, n);
}
But what if the string was more than one char? How would the code look differently? Thanks for any help.
If it were a "string" instead of a char, you would do something like this:
#include <stdio.h>
void AFunc (char *myStr, int *myNum) {
myStr[0] = 'p'; // or replace the lot with strcpy(myStr, "pax");
myStr[1] = 'a';
myStr[2] = 'x';
myStr[3] = '\0';
*myNum = 9;
}
int main (void) {
char someString[4];
int n = 6;
AFunc(someString, &n);
printf("%s %d", someString, n);
return 0;
}
which outputs:
pax 9
A "string" in C is really an array of characters terminated by the \0 (NUL) character.
What the above code does is to pass in the address of the first character in that array and the function populates the four characters starting from there.
In C, a pointer to char isn't necessarily a string. In other words, just because you have char *x;, it doesn't mean that x is a string.
To be a string, x must point to a suitably allocated region which has a 0 in it somewhere. The data from the first character that x points to and up to the 0 is a string. Here are some examples of strings in C:
char x[5] = {0}; /* string of length 0 */
char x[] = "hello"; /* string of length 5, the array length being 6 */
char *x = "hello"; /* string of length 5. x is a pointer to a read-only buffer of 6 chars */
char *x = malloc(10);
if (x != NULL) {
strcpy(x, "hello"); /* x is now a string of length 5. x points
to 10 chars of useful memory */
}
The following are not strings:
char x[5] = "hello"; /* no terminating 0 */
char y = 1;
char *x = &y; /* no terminating 0 */
So now in your code, AFunc's first parameter, even though is a char * isn't necessarily a string. In fact, in your example, it isn't, since it only points to a memory that has one useful element, and that's not zero.
Depending upon how you want to change the string, and how the string was created, there are several options.
For example, if the myStr points to a writable memory, you could do something like this:
/* modify the data pointed to by 'data' of length 'len' */
void modify_in_place(char *data, size_t len)
{
size_t i;
for (i=0; i < len; ++i)
data[i] = 42 + i;
}
Another slightly different way would be for the function to modify data until it sees the terminating 0:
void modify_in_place2(char *data)
{
size_t i;
for (i=0; data[i]; ++i)
data[i] = 42 + i;
}
You are only dealing with chars and char pointers. None of the char pointers are valid strings as they are not null terminated.
Try defining a string and see what it looks like.
But what if the string was more than one char? How would the code look
differently? Thanks for any help
Ofcourse, you would modify the other characters as well, but in the exact same way you did the first time.
Declare a char array and pass its address
Modify values at those address
A char array would be a more clear term for a string.