I am writing a programme..and it's so simple but I get this warning all the time.
warning format ‘ d’ expects type ‘int ’ but argument 2 has type ‘char *’
What could have gone wrong?
Printable View
I am writing a programme..and it's so simple but I get this warning all the time.
warning format ‘ d’ expects type ‘int ’ but argument 2 has type ‘char *’
What could have gone wrong?
The warning explains it :)Quote:
Originally Posted by zel
Basically, you probably wrote something like this:
So the compiler complains that the argument corresponding to %d is not an integer. How to fix this depends on what you are trying to do.Code:#include <stdio.h>
int main(int argc, char *argv[])
{
printf("My name is %d\n", argv[0]);
return 0;
}
oops..ok I found the mistake thx 2 u. nice :)
Ok. How about this one?
My warnings are:
In function ‘main’:
line 11: warning: assignment makes integer from pointer without a cast
line 12: warning: assignment makes integer from pointer without a cast
line 20: warning: assignment makes integer from pointer without a cast
line 21: warning: assignment makes integer from pointer without a cast
the purpose of the program is this:Code:1 #include <stdio.h>
2. #include <string.h>
3. #include <stdlib.h>
4.
5. int main (int argc,char *argv[]){
6.
7. char x,y;
8. char *pX, *pY;
9. char str1[20],str2[20];
10.
11. x = (char *) malloc(20* sizeof(char) );
12. y = (char *) malloc(20* sizeof(char) );
13.
14. scanf("%19s",str1);
15. scanf("%19s",str2);
16.
17. pX=&x;
18. pY=&y;
19.
20. x=&str1;
21. y=&str2;
22.
23. printf("x= %c and y= %c", *pX,*pY);
24.
25. return (0);
26.
27.}
Write a program which:
-declares 2 variables of pointer-to-character-type
-holds dynamic memory for 20 positions of memory for each variable
-initializes the variables by reading 2 strings from keyboard
-prints the values of the 2 variables on screen.
What am I doing wrong? Can u help me understand it?
Well, why are you trying to assign the return value of malloc to a non-pointer variable? Likewise, why are you trying to assign the address of an array to a non-pointer variable?
so it has to be:
[CODE][
pX = (char *) malloc(20* sizeof(char) );
pY = (char *) malloc(20* sizeof(char) );
/CODE]
right?
in the next lines should it also be:
?Code://pX=&x;
//pY=&y;
pX=&str1;
pY=&str2;
how i'm going to print the value of str1 and str2 by giving it in the variables pX and pY ?
You only need pX and pY. After you have allocated space with malloc and assigned the return values of the malloc calls to them, they would point to the first character of a dynamic array of char, which you can thus use as a string.
Remember to free what you malloc.
(Of course, if you are always allocating exactly 20 characters, then you will find that dynamic memory allocation is quite unnecessary.)
ok I also added 2 free.
I'm confused at the 2nd part of your answer. you mean I don't have to use str1 and str2 but pX and pY instead?Code:free(pX);
free(pY);
ps.sorry if my English is bad
Yes.Quote:
Originally Posted by zel
char *px = malloc(20 * sizeof(char));
Ok problem solved. Thank u again :)
ok here's another problem. I want to copy the first N characters from a string1 to a string2 and erase the rest characters of string1 BUT without using any string function from <string.h>.
So if
string1= bananas
string2=boat
and N=2
at the end it will be:
string1=bo
string2=boat
how can I make this happen?
I found the length of string1 and then I want to swap the substring with the string1..But I'm stuck here.Any ideas?
Code:void copy( char* str1,char* str2, int N) {
int i,len1;
char *temp;
for (i=0; str1[i] != '\0'; i++){
len1= i;
}
for (i=0;i<N;i++){
*temp=*str1;
*str1=*str2
*str2=temp;
}
printf("%19s\n",str1);
}
Well, if you regard your code, temp is uninitialized. Points to neverland.
But let's formulate an algorithm for you want to do and start from there:
Truncate string 1.
Copy N characters from string 2 to string 1.
Null terminate string 1 (otherwise it won't be a string.)
Try that.
Also, printf does not need the size of the string to print it. So %19s should be %s.
You might want to look at the documentation of strncpy() for inspiration. Pay attention to it's function parameters, and description of how it works. This is what you are trying to implement.
Have fun with reverse engineering.
There's no reason to copy strings, he could just swap the pointer values...
Code:temp = str1;
str1 = str2;
str2 = temp;
There are several problems with that.
First, those changes will be local to the function. That's not the point of the whole.
Secondly, zel wants to truncate everything in the string after N characters. Your code does not take that into account, though it is possible.
Thirdly, only N characters from the source should be copied to dst. The source shall remain unaffected!
So I did this:
it compiles with no problems,but if I run it and put for example "nikos" and "bananas", the result of string1 for N=3 is "banos" instead of "ban". Why does this happen? I thought I null-terminated the string.Code:void copy( char* str1,char* str2, int N) {
int i,len1,len2;
for (i=0; str1[i] != '\0'; i++){
len1= i;
}
for (i=0; str2[i] != '\0'; i++){
len2= i;
}
if (i!= N){
for (i=0;i<N;i++){
if (len2<N){
str1[i]= '\0';
}
str1[i]=str2[i];
}
}
else {
str1[i]='\0';
}
printf("%s\n",str1);
}
I think my if-else is the problem. I have to check it somehow else.
_________
EDIT:
I think I should be more careful sometimes.Haha.I found my mistake and did like this:
I'm happy now ! :DCode:for (i=0; str1[i] != '\0'; i++){
len1= i;
}
for (i=0; str2[i] != '\0'; i++){
len2= i;
}
while (i!= N){
for (i=0;i<N;i++){
if (len2<N){
str1[i]= '\0';
}
str1[i]=str2[i];
}
}
for (i=N;i<len1;i++){
str1[i]='\0';
}
I suggest that you indent your code properly. I read:Quote:
Originally Posted by zel
I do not understand why you compare i != N in the first place: it looks wrong since when i == N, i.e., when the length of the second string is equal to N, you only null terminate the first string at index i, without copying.Code:if (i != N) {
for (i = 0; i < N; i++) {
if (len2 < N) {
str1[i] = '\0';
}
str1[i] = str2[i];
}
}
else {
str1[i] = '\0';
}
Looking at your for loop, you assign a null character to str1[i] if len < N. But even when this happens, you immediately overwrite it with str2[i].
I think you should keep things simple: do not determine the length of either of the strings. Rather, just start copying until you reach either the end of the second string, or your counter reaches N, whichever comes first. When that happens, null terminate the first string at that point.
Oh sorry. I didn't see your answer. Is my code now correct?
Test before asking. Even if it is correct, it looks like a Rube Goldberg machine. :)Quote:
Originally Posted by zel
Haha. I see that.
OK.
it runs properly except :
1) that if eg. len2=5 and I give 5 as N,it does no changes.
and
2)if eg. len2=5 and I give 10 ,it never goes out of the loop.
gr..I made it too complicated you think?
Yes, too complicated. You only need one loop, and you do not even need another if statement.
Ok, it was that simple and I've stuck here for so much time...:$
I don't know why I think so complicated. THANK YOU very MUCH for your help. You really made me think. Thanks again.It works in each case. :)Code:for (i=0;i<N && str2[i]!='\0';i++){
str1[i]=str2[i];
}
str1[i]= '\0';
Yes, that is pretty much what I had in mind :)
Still, you should indent the code properly, and I recommend adding some whitespace for formatting, e.g.,
Code:for (i = 0; i < N && str2[i] != '\0'; i++) {
str1[i] = str2[i];
}
str1[i] = '\0';
Yes I'm gonna fix that. Thank u again :)