why strcat producing this error? - c

[Warning] passing argument 1 of 'strcat' makes pointer from integer without a cast THIS ERROR IS COMING WHEN I AM USING STRCAT WHAT SHOULD I DO , below is my code
#include <stdio.h>
#include <string.h>
void main() {
char a[20], b[256], p = NULL, f;
int i, j, n, k, c[20], t, x, l;
printf("enter the no of possible characters");
scanf("%d", &k);
printf("enter the possible characters in the dictionary");
printf("hi");
for (i = 0; i < k; i++) {
c[i] = (i);
a[i] = getchar();
}
for (i = 0; i < k; i++) {
printf("%d %s\n ", c[i], a[i]);
}
l = k;
printf("enter the string\n");
scanf("%s", &b);
n = strlen(b);
printf("%d", n);
for (i = 0; i < n; i++) {
strcat(p, b[i]);
}
getch();
}

This is because strcat takes null-terminated strings, not individual characters. You can work around this problem by null-terminating b at position n, and calling strcat once:
b[n] = '\0';
strcat(p, b);
This will append the initial n characters of b to p, all in one go. Of course, given that p does not have any characters in it, this is completely pointless: you might as well use strcpy. Of course, you need to declare p to be a character buffer large enough to hold all of b:
char a[20],b[256],p[256]={0},f;
// ^^^^^^^^^
b[n] = '\0';
strcpy(p, b);

Your variable definition is wrong.
char a[20],b[256],p=NULL,f;
does not define p as a pointer. It is a char variable with is not the expected type for the first argument of strcat().
You need to define p as a pointer to a string which has enough memory to hold the concatenated result.
That said,
strcat(p,b[i]);
is completely wrong, because b[i] is not a pointer to a string, either.

Related

what is difference between printf and putchar?

Here is my code
void display(char ch, int lines, int width);
int main()
{
char c;
int rows, cols;
while (1)
{
scanf("%c %d %d", &c, &rows, &cols);
display(c, rows, cols);
if (c == '\n')
break;
}
}
void display(char ch, int lines, int width)
{
for (int i = 0; i < lines; i++)
{
for (int j = 0; j < width; j++)
printf("%c", ch);
printf('\n');
}
}
I wana like this.
if I input a 2 3
It returns
aaa
aaa
But It didn't work. SO I change like this
void display(char ch, int lines, int width)
{
for (int i = 0; i < lines; i++)
{
for (int j = 0; j < width; j++)
putchar(ch);
putchar('\n');
}
}
It works well. why that code works well??
what is difference between printf and putchar?
printf('\n'); is a mistake, and your compiler should warn you about it. The first argument to printf should be a string, such as "\n", not a character constant, such as '\n'.
The source code "\n" represents a string of two characters, the first being a new-line character and the second being a null character that indicates the end of the string. When used in an expression this way, it is automatically converted to a pointer to its first element, and this pointer is passed to printf.
The source code '\n' represents a character. Its value is the code for that character. When it is passed to printf, that value is passed. That is the wrong thing to pass to printf, which is why your first program did not work.
When you have %c inside the printf, then it will print the character only. But your point is you gave 2 but it prints a, why? see the ASCII table and find the number to char equivalent value from here ASCII Table. on the other hand putchar is just char printer.

Adding strings to 2D array in C

I'm trying to write a program that looks for the first empty space in a 2D array and adds a custom string to that space. I have tried some things that i found on the internet but none seem to work or match my specific scenario. This is it:
#include <stdio.h>
#include <string.h>
int tags[10] = {1,2,3,4,5};
char owners[10][10] = {"per1", "per2", "per3", "per4", "per5"};
int tagAdd;
char ownerAdd;
int i;
int addBool;
int j;
int len;
int main()
{
printf("Enter the tag ID you want to add: ");
scanf("%d", &tagAdd);
printf("Enter the tag owners name: ");
scanf("%d", &ownerAdd);
len = strlen(ownerAdd);
while (i<10)
{
if (tags[i] == 0)
{
tags[i] = tagAdd;
owners[i][len] = ownerAdd; //This is the part I can't figure out
addBool = 1;
}
if (addBool == 1)
{
break;
}
i++;
}
i = 0;
addBool = 0;
len = 0;
while (i<10)
{
printf("tag[%d]", tags[i]);
len = strlen(owners[i]);
printf(" is owned by ");
while (j < len)
{
printf("%c", owners[i][j]);
j++;
}
printf("\n\r");
i++;
j = 0;
}
}
You cannot do this:
char ownerAdd;
scanf("%d", &ownerAdd);
len = strlen(ownerAdd);
You are passing the incorrect types. ownerAdd is a single char, scanf
expects with %d a pointer to int, you are passing a pointer to char and if
scanf converts the value, it will overflow. And strlen expects a char*
which points to a valid string (0-terminated). You are doing all this wrong.
This would be correct:
char ownerAdd[100];
scanf("%99s", ownerAdd);
len = strlen(ownerAdd);
And for replacing a value:
owners[i][len] = ownerAdd; //This is the part I can't figure out
is also wrong, because owners[i] is a char[10], you have to do:
strncpy(owners[i], ownerAdd, sizeof owners[i]);
owners[i][sizeof(owners[i]) - 1] = 0;
to copy the string.
The next error is that you don't initialize i and do (the first loop)
while (i<10)
{
...
}
this is going to fail. Same thing with j, it is uninitialized.

The first character in both string won't swap in C [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
#include <stdio.h>
#define SIZE 101
void swap(char *a, char *b);
int main(void) {
char string1[SIZE];
char string2[SIZE];
printf("please enter string 1 :");
scanf("%s", string1);
printf("please enter string 2 :");
scanf("%s", string2);
swap(string1, string2);
printf("string 1 is %s, string 2 is %s\n", string2, string1);
}
void swap(char *a, char *b) {
int i;
char temp[101];
for (i = 0; i < SIZE; i++) {
temp[i] = *a;
*a = *b;
*b = temp[i];
}
}
When I give inputs to both string1 and string2 lets say "yes" and "hello", it will print string1 is yello and string2 is hes. I've tried to modify by changing *b to *[b+1] and *a to *[a+1] and it gets the first character to work but now the second character won't swap.
EDIT
Also I've tried another function but the problem is still there.
void swap(char* a, char* b)
{
char temp;
temp=*a;
*a=*b;
*b=temp;
a++;
b++;
}
Your swap function is wrong:
Here is the corrected version:
void swap(char* a, char* b)
{
int i; char temp;
for (i = 0; i<SIZE; i++)
{
temp = a[i];
a[i] = b[i];
b[i] = temp;
}
}
you don't need a temp array, just one char is enough for the temporary.
you need to access the different chars of the input a and b, and not only the first one.
BTW: here is a better version of swap where you pass the size as parameter:
void swap(char* a, char* b, int size)
{
int i; char temp;
for (i = 0; i < size; i++)
{
temp = a[i];
a[i] = b[i];
b[i] = temp;
}
}
Actually only the first characters of your strings is swapped(which makes sense as mentioned in other answers), you don't see this because you change the order of printing with
printf("string 1 is %s, string 2 is %s\n", string2, string1);
^^^^^^^^^^^^^^^
Here you are always just swapping the first character since you a and b always point to first element.
for(i=0; i<SIZE; i++)
{
temp[i]= *a;
*a=*b;
*b=temp[i];
}
you can use array subscript
int i; char temp;
for (i = 0; i < SIZE; i++)
{
temp = a[i];
a[i] = b[i];
b[i] = temp;
}
There are multiple problems in your code:
you print the strings in the wrong order
you only swap the first character, and you do this SIZE times. SIZE is odd so you see it swapped, the previous bug makes it look like the other characters were swapped. If SIZE were even, you would not swap anything and the previous bug would let you erroneously believe everything is fine...
Less important, but worth mentioning:
you do not check the return values of scanf()
you do not return 0 at the end of main().
Here is a corrected and improved version:
#include <stdio.h>
#define SIZE 101
void swap(char *a, char *b, int size) {
for (int i = 0; i < size; i++) {
char temp = a[i];
a[i] = b[i];
b[i] = temp;
}
}
int main(void) {
char string1[SIZE];
char string2[SIZE];
printf("please enter string 1 :");
if (scanf("%s", string1) != 1)
return 1;
printf("please enter string 2 :");
if (scanf("%s", string2) != 1)
return 1;
swap(string1, string2, SIZE);
printf("string 1 is %s, string 2 is %s\n", string1, string2);
return 0;
}
Try to use parentheses like *(a+1) instead of bracket *[b+1]. In your code
change the following
for(i=0; i<SIZE; i++)
{
temp[i]= *a;
*a=*b;
*b=temp[i];
}
with
for(i=0; i<SIZE; i++)
{
temp[i] = *(a+i);
*(a+i) = *(b+i);
*(b+i) = temp[i];
}
Final code is here:
#include <stdio.h>
#define SIZE 101
void swap(char* a, char* b);
int main(void)
{
char string1[SIZE]; char string2[SIZE];
printf("please enter string 1 :");
scanf("%s",string1);
printf("please enter string 2 :");
scanf("%s",string2);
swap(string1, string2);
printf("string 1 is %s, string 2 is %s\n",string1,string2);
return 0;
}
void swap(char* a, char* b)
{
int i; char temp[101];
for(i=0; i<SIZE; i++)
{
temp[i] = *(a+i);
*(a+i) = *(b+i);
*(b+i) = temp[i];
}
}

Assign first character of a char array to another variable

I would like to grab a single character from a char array and move it into another character array. It is driving me crazy that i am having so much difficulties with something so simple.
My code is meant to reverse a string 2 values at a time. I have a string (ABCDEFGH) and i want to separate it into two strings (ACEG & BDFH). How would i go about doing this?
Currently, i have this:
char *hexrev(char str[]){
int i = 0;
char a[256];
char b[256];
int len = strlen(str);
for(i; i<len-1; i+=2)
{
printf("str[i] : %c\n", str[i]);
a[0] = str[i];
b[0] = str[i+1];
a[1] = '\0';
b[1] = '\0';
printf("A : %s\n", a);
printf("B : %s\n", b);
}
return str;
}
By changing sizeof(str) to strlen(str) and terminating a & b, I was able to get the code to work. Thank you!
You cannot use sizeof on an array inside a function. You must either use strlen() (once, preferrably) to compute the length, or pass it in as an extra argument.
Also you cannot legally print a and b as strings since you never ensure they're properly terminated.
Third, you only ever write to the first characters of a and b, so they will never grow longer than 1 character.
You should show a slightly more complete example, at least including the code that does the call to your function.
int i = 0, j = 0;
char a[256];
char b[256];
int len = strlen(str);
for(i; i<len-1; i+=2){
a[j] = str[i];
b[j++] = str[i+1];
}
a[j] = b[j] = '\0';
printf("A : %s\n", a);
printf("B : %s\n", b);
#include <stdio.h>
int main(void){
char str[] = "ABCDEFGHI";//In the case of odd-length
char a[256]={0};//letter of the index of odd numbered.(odd : 1 origin)
char b[256]={0};//for even
int i;
char *p[] = {a, b};
for(i = 0; str[i]; ++i){
*p[i & 1]++ = str[i];
}
printf("A : %s\n", a);//ACEGI
printf("B : %s\n", b);//BDFH
return 0;
}

Reverse char array with pointer in C

I want to reverse a char array using pointers, but all I get when I printf the pointer is null. I don't know what I'm doing wrong or how to fix it. So how can I reverse the string in a similar way?
#include <stdio.h>
void reverse(char *cstr);
int main()
{
char a[100];
char *p = a;
printf("geef een string "); // ask user to write a word
scanf("%s", &a);
reverse(p);
printf("%s", *p);
}
void reverse(char *p)
{
int i = 0;
char temp;
int lengte;
for(i = 0; *(p+i) != '\0'; i++)
{
lengte++; // length of char array without the '\0'
}
for(i = 0; i < lengte; i++)
{
temp = p[i]; // something goes wrong here but I don't know what
p[i] = p[lengte-i];
p[lengte-i] = tem;
}
}
Something goes wrong at the
p[i] = p[lengte-i];
p[lengte-i] = tem;
part. What do I need to change it to?
Two adjustments:
replace
printf("%s", *p);
with
printf("%s", p);
because printf is expecting a pointer, not a dereferenced pointer,
and
for(i = 0; i < lengte; i++)
with
for(i = 0; i < lengte--; i++)
because your counting of the length in the loop before that one ends up with one char too many. Hence the \0 is placed at the beginning of the string.
$ gcc test.c && ./a.out
geef een string 1234
4231$

Resources