I am at the very initial stage of learning programming. I working with a program that uses self-made function. I don't understand my mistakes. I would be grateful for your help. Please, do me a favor and answer using methods that are commensurate with the primitive stage I am at. I am leaving comments I've written, so you can see what I am trying to achieve by this or that code line.
/* Prints a user's name */
#include <stdio.h>
#include <string.h>
// prototype
void PrintName(char name);
/* this is a hint for the C saying that this function will be later specified */
int main(void)
{
char name[50];
printf("Your name: ");
scanf ("%49s", name); /* limit the number of characters to 50 */
PrintName(name);
}
// Says hello to someone by name
void PrintName(char name)
{
printf("hello, %s\n", name);
}
I get these error messages:
function0.c: In function ‘main’:
function0.c:14: warning: passing argument 1 of ‘PrintName’ makes integer from pointer without a cast
function0.c: In function ‘PrintName’:
function0.c:21: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
function0.c:21: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
The function PrintName is based on the previous program I took from the course (and adopted it to C):
#include <stdio.h>
#include <string.h>
int main (void)
{
char name[40];
printf ("Type a name: ");
scanf ("%39s", name);
printf ("%s", name);
printf("\n");
}
This last program works perfectly.
What mistake do I make in my original program? If I understand correctly, there is a problem with my PrintName function.
The initial program that prints a name is a modified version of CS50 program that uses CS50 library:
// Prints a user's name
#include <stdio.h>
#include <cs50.h>
// prototype
void PrintName(string name);
int main(void)
{
printf("Your name: ");
string s = GetString(); //GetString is the same as scanf; it takes input from the user
PrintName(s);
}
// Says hello to someone by name
void PrintName(string name)
{
printf("hello, %s\n", name);
}
Given "string" is "char" in C, I replace string with char in my program.
Thank you!
You should use char* instead of char, as an argument of your function. Char is one symbol, while char* is pointer to a string.
Your function PrintName is waiting for a char as parameter but you give a char[] that's why you see this :
warning: passing argument 1 of ‘PrintName’ makes integer from pointer without a cast
To give a char[] as parameter you need to change your function like this :
void PrintName(char *name);
Change void PrintName (char name); to void PrintName (char *name); or to void PrintName (char name[]);
Currently this function receives one character called name. You want it to receive an array of chars.
void PrintName(char name);
You function expects a character variable to be passed but instead you pass a char array. Thus it leads to error.
Moreover, %s in printf will expect a char * and you pass a char,thus leading to another problem.
To correct you program declare and define function with parameter as follows-
void PrintName(char *name);
or
void PrintName(char name[]);
Both will work.
Related
Trying to do cs50 without the cs50 GetString.
Got stuck by doing the new function included in the following code :
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
void PrintName(char name);
{
printf("Your name is %c\n", name);
}
int main();
{
char fio[10];
printf("Hello, ");
scanf("%c", &fio);
PrintName(fio);
return 0;
}
Says the next:
hello-0.c:9:1: error: expected identifier or '('
{
^
hello-0.c:14:1: error: expected identifier or '('
{
^
What it could be?
First things first, you have extra semi-colons: in front of main and PrintName function. Remove them.
Secondly, you created char array (aka string) and you scanf-ed it wrong. If you want to get name as string (not as a char, as you did), you have to do it like this:
char fio[10];
printf("Hello, ");
scanf("%9s", fio);
Note that, as I am reading string, my format is %s (note 9 in there to read up to 9 characters, because you have array of 10). Moreover, I pass the address of my char array (which is already an address). That's how you read a string. And printing it in function will be:
void PrintName(char name[])
{
printf("Your name is %s\n", name);
}
where we pass char[] to function and again print with %s format
#include <stdio.h>
#include <string.h>
int myprint(char_array){
char mystring[80];
strcat(mystring, "\n");
printf("%s", mystring);
return 0;
}
int main(int argc, char** argv){
int count = 5;
char letter = 'c';
printf("decimal: %d, char: %c\n", count, letter);
myprint("sup");
return 0;
}
I get warnings on compile:
cchilders:~/projects/buildyourownlisp_in_C/ch3 [master]$ compile basics.c basics
basics.c: In function ‘myprint’:
basics.c:4:5: warning: type of ‘char_array’ defaults to ‘int’
int myprint(char_array){
^
It compiles, but my myprint function doesn't work:
cchilders:~/projects/buildyourownlisp_in_C/ch3 [master]$ ./basics
decimal: 5, char: c
I see this answer warning: return type defaults to ‘int’ [-Wreturn-type] but doesn't apply to me since I did declare int main(...)
I also see this declaration of functions:
return_type function_name( parameter list ) {
body of the function
}
And for myprint I declare as taking int and return 0. What does this warning mean and why doesn't my function work? Thank you
ANSWER:
void myprint(char mystring[]){
strcat(mystring, "\n");
printf("%s", mystring);
}
quiets the warnings, but causes Segmentation fault (core dumped)
Changing to
void myprint(char[] mystring){
strcat(mystring, "\n");
printf("%s", mystring);
}
makes it worse:
cchilders:~/projects/buildyourownlisp_in_C/ch3 [master]$ cc -std=c99 -Wall basics.c -o basics
basics.c:4:21: error: expected ‘;’, ‘,’ or ‘)’ before ‘mystring’
void myprint(char[] mystring;){
^
basics.c: In function ‘main’:
basics.c:15:5: warning: implicit declaration of function ‘myprint’ [-Wimplicit-function-declaration]
myprint("sup");
^
I also tried
void myprint(char[] mystring;){...
and
void myprint(char[] mystring,){...
As others have pointed out, you didn't specify a type for char_array, so it is assumed to be int. Changing it to char char_array[] fixes this.
Your other problem is that you're passing a string constant ("sup") to this function and are then attempting to modify it. String constants are stored in a read-only section of memory, so you can't modify it.
Given that you're only printing the string with a newline, you can do this instead:
void myprint(char mystring[]){
printf("%s\n", mystring);
}
You are not providing a data type for char_array in
int myprint(char_array)
You need char * or whatever you want it to be.
Firstly, function definitions should be like
return-type function-name ( parameter-type parameter-name, parameter-type parameter-name)
{ ... }
You did not specify either a parameter type or a parameter name. If you mean char_array to mean a type, you need to define it first, using a typedef or a struct or something else. If you mean char_array to be a parameter name, you need to specify its type, as
char[] char_array
say. Also, in this case, you do not actually use the variable char array anywhere in the function myprint. So the argument "sup" is not being used at all.
After edit to the question:
Try
char str[] = "sup";
myprint(str);
instead. As far as I know, you can't pass a string (a character array) by value.
int myprint(char_array)
What type has the parameter with name char_array? Because you didn't specify it, the compiler assumed it to be an int. Luckily it's warning you about that.
Don't rely on such behaviour, though. (I don't know whether this is still legal in C11 for example) Just write correct function declarations, including parameter types.
You need to specify the type of the parameters you expect to be passed to the function. There are mistakes in the function too. char_array is of char* type. You need to copy every part of the passed array to your local array, THEN only can you call printf for this function to work
I'm trying to read a string and print it in Linux using:
cc child.c -o child
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
char st[100];
scanf("%s", &st);
printf("%s", st);
return 0;
}
However I encounter this warning that will not allow me to compile.
child.c: In function ‘main’:
child.c:8:2: warning: format ‘%s’ expects argument of type ‘char ’, but argument 2 has type ‘char ()[100]’ [-Wformat=]
scanf("%s", &st);
How can I solve it? Thanks!
In C, when passed to a function, array names converted to pointer to the first element of array. No need to place & before st. Change
scanf("%s", &st);
to
scanf("%s", st);
In C array types degenerate to pointers. So when you take the address of an array, you actually get a pointer to a pointer. Just pass the array itself to scanf.
So I am writing a simple code to print out each symbol in my string. When compiling it, it gives me an error tough that I do not understand:
The code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void) {
char my_string[50];
int i, n;
printf("Type in a string please : ");
scanf("%s", &my_string);
n = strlen(my_string);
for (i = 0;i < n; i++) {
printf("%c",my_string[i]);
}
}
The error it gives:
gcc yl2.c -o Yl2
yl2.c: In function ‘main’:
yl2.c:9:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[50]’ [-Wformat=]
scanf("%s", &my_string);
^
What is the problem here?
scanf("%s", &my_string); there should not be a &.
Since you have declared char my_string[50]; my_string as character array which is of type char * which is expecting in scanf() as the warning states.
Just use, scanf("%s", my_string);. Base address of the array as argument is sufficient.
scanf("%s", &my_string); should be scanf("%s", my_string);
my_string is an array, which decays to a pointer when you type it out without [ ].
If you type &my_string, you get an array pointer, which is strictly speaking not the same thing. That's why the compiler complains.
&my_string is a pointer to my_string i.e. a ‘char (*)[50]’
You can either use
scanf("%s", &my_string[0]);
or more conventionally
scanf("%s", my_string);
remove & sign in scan, it will work
Also no need to use string.h here
I'm working on my assignment for my C course, and I'm trying to take in the user's input and store it in a variable to use for later in my code. Here's what my main function looks like,
int main() {
// Variables here
char* inputLine[10];
do {
printf("Insert number....");
scanf("%s\n", inputLine);
// More stuff here
}
return 0;
}
This code gives me a bunch of warnings, warning: format specifies type 'char *' but the argument has type 'char **' [-Wformat], and if I change the variable declaration to,
char* inputLine = NULL;
When I execute my code I get a seg fault, can someone explain to me what I am doing wrong, and the differences of what happens in the memory when I'm initializing this variable?
char* inputLine[10];
--> is an array of ten pointers to char
printf's format %s expects argument of type char *, but you're providing it as type char **
Just use
char inputLine[10];
To avoid possible buffer overflow you should use
scanf("%9s", inputLine); //Notice the size with %s
9 only because C string are null terminated ('\0') so one extra byte for it goes at end
char inputLine[10];
do {
printf("Insert number....");
scanf("%9s\n", inputLine);
// More stuff here
} while( //some condition);
However if you edit your code and remove * you get answer, but normal array deprecated, nowdays, programmers use vector, normal array in C not safe :
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<string> inputLine;
You can define with every data type:
vector<int> myvar;
Or you can define multidimensional vector:
vector< vector <int> > myvar;