How to fix error Format specifies type 'char *' but the argument has type 'char' - objective-c

I get a warning saying:
"Format specifies type 'char *' but the argument has type 'char'" for the student variable. I am copying/pasting the code out of a book into xcode and am not sure how to fix this. The only thing that prints in the console is "(lldb)". Any advice
#include <stdio.h>
void congratulateStudent(char student, char course, int numDays)
{
printf("%s has done as much %s Programming as I could fit into %d days.\n", student, course, numDays);
}
int main(int argc, const char * argv[])
{
// insert code here...
congratulateStudent("mark", "Cocoa", 5);
return 0;
}

void congratulateStudent(char *student, char *course, int numDays)
the %s means that you are going to print a string ( array of chars)
and char student this means that student is a char type
so student here is not a pointer to a string
In order to change the student type from char to a string pointer you have to add asterisk to student char *student
In your code you are calling the congratulateStudent with input parameter string "mark". So to support this string the input parameter student should be defined as pointer of string
so you are missing the asterisk in the definition of student
The same thing for course

void congratulateStudent(char *student, char *course, int numDays)
Use Function signature like because you are passing string as argument to function in main but function has character type argument..

Related

Can't put a space between output and input on the same line

After printing, if I write the name I get:
Insert a name :Andrea
There is no space before Andrea, even if I put the space in the output. How can I add a space before writing the name?
#include <stdio.h>
int main() {
int string[8];
printf("\nInsert a name : ");
printf(" ");
scanf("%s", string);
printf("\nThe name is : %s", string);
return 0;
}
The code that you provided does not compile because the variable string is an array of integers (and not a string as you might think). And when you input the value of it, you're telling the compiler to expect a string ("%s" in scanf("%s", string")).
Change your string declaration to char string[8] and it should work (with the spacing you want):
Note that the value that goes into string will not contain more than 8 characters.
Actually, you did a small mistake in your code, you declared the string to be an array of int & from the code scanf("%s", string") the compiler expects from the user to enter a string as it contains %s as the format specifier. Hence, the code doesn't compile.
In order to correct this, you should declare string as char string[8] (string containing 8 characters).
The correct code is given below :
#include <stdio.h>
int main() {
char string[8];
printf("\nInsert a name : ");
scanf("%s", string);
printf("\nThe name is : %s", string);
return 0;
}
From the above correction your problem for space between input and output will be solved!
Here, is the output which is received from this correction :
I hope so this explanation will be helpful for you!
If any problem persists then feel free to ask in comments! ;-)

Overloading function in C++ as unsigned char and unsigned int result in ambiguous

I have to overloaded functions:
void wypisz(unsigned int32 x, int n = 1);
void wypisz(unsigned char x, int n = 1);
here is code where I rise them:
main()
{
wypisz((int32)(32), 7);
wypisz('a', 7);
return 0;
}
and when I try to compile it using G++ I get an error:
test.cpp: In function 'int main()':
test.cpp:10:21: error: call of overloaded 'wypisz(int, int)' is
ambiguous wypisz((int)(32), 7);
test.cpp:10:21: note: candidates are:
test.cpp:5:6: note: void wypisz(unsigned int, int) void wypisz(unsigned int x, int n = 1);
test.cpp:6:6: note: void wypisz(unsigned char, int) void wypisz(unsigned char x, int n = 1);
When I remove unsigned it will compile fine.
Is there a way to call this method - wha tI should change in the call statement? Unfortunatelly I can not change anything in the declaration = they must stay as they are.
The problem is that, in your function call, you are casting to an int32, which is neither an unsigned char nor an unsigned int32. In fact, it is implicitly castable to either of them (the compiler can convert it automatically). However, because it can convert it to either automatically, it doesn't know which to convert it to, and that is why the call is ambiguous.
To call the method unambiguously, just cast it to an unsigned int32:
wypisz((unsigned int32)32, 7);
Now, the compiler doesn't have to do any implicit casting, since there is an exact match. Instead, it just calls the right function.

Copy two dimensional arrays using pointers

I want to copy two dimensional arrays and I made this function but it caused a compilation error
void Cpy_2d_arr(unsigned char *from,unsigned char *to)
{
unsigned char col,row;
for (row=0;row<4;row++)
{
for(col=0;col<4;col++)
{
(*(*(to+row)+col)) = (*(*(from+row)+col));
}
}
}
The two dimensional arrays are
unsigned char arr[4][4] = {'7','8','9','-','4','5','6','*','1','2','3','-','c','0','=','+'};
the target is an array inside a struct with the same size
the errors are :
1- Error 8 expected 'unsigned char ' but argument is of type 'unsigned char ()[4]
2- Error 11 subscripted value is neither array nor pointer
3- Error 11 invalid type argument of unary '*' (have 'int')
char arr[4][4] = {'7','8','9','-','4','5','6','*','1','2','3','-','c','0','=','+'};
is an array of pointers
void Cpy_2d_arr(unsigned char *from,unsigned char *to)
receives an single pointer( e.g. to an array of chars).
the types of your parameters are not compatible, change it to
void Cpy_2d_arr(unsigned char from[4][4],unsigned char to[4][4])
it should work just fine

ERROR: Data argument not used by format string

Today I'm writing this program and I had has two problems.
This is the full code in Objective-C for a OS X Project:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
#autoreleasepool {
int numx;
int quadr;
NSLog(#"Inserisci un numero");
scanf("%i", &numx);
quadr = numx * numx;
NSLog(#"Il quadrato del tuo numero %i è: ", numx, quadr);
}
return 0;
}
The second NSLog reports this error: Data argument not used by format string. Why it create this kind of error? In which way I can solve this error?
More over the run-time of the program finish unexpectly after that the output has written: "Inserisci un numero". When I insert the number requiry, the program not display the number squared as required from the second NSLog. Why I'm having this interrupt? Help me please. I what to understand what is happenend.
The warning you get on that line is correct.
The quadr parameter you pass is not used in the format string. Each parameter you pass need to have a % equivalent in the format string.
Specify the extra parameter in your code sample conform line below:
NSLog(#"Il quadrato del tuo numero %i è %i", numx, quadr);

XOR reverse a string in objective-c get an error

I want to use the following code to reverse a char * type string in objective-c:
- (char *)reverseString:(char *)aString
{
unsigned long length = strlen(aString);
int end = length - 1;
int start = 0;
while (start < end) {
aString[start] ^= aString[end];
aString[end] ^= aString[start];
aString[start] ^= aString[end];
++start;
--end;
}
return aString;
}
But I got an error EXC_BAD_ACCESS at this line
aString[start] ^= aString[end]
I googled and found people said I can't modify a literal string because it is readonly. I am new to C so I wonder what simple data type (no object) I can use in this example? I get the same error when I use (char []) aString to replace (char *) aString.
I assume you're calling this like
[myObj reverseString:"foobar"];
The string "foobar" here is a constant literal string. Its type should be const char *, but because C is braindead, it's char *. But it's still constant, so any attempt to modify it is going to fail.
Declaring the method as taking char[] actually makes no difference whatsoever. When used as a parameter type, char[] is identical to char*.
You have two choices here. The first is to duplicate the string before passing it to the method. The second is to change the method to not modify its input string at all but instead to return a new string as output. Both can be accomplished using strdup(). Just remember that the string returned from strdup() will need to be free()'d later.