Why is my program not compiling? - c

My code is suppose to initialise any word you type but it is refusing to compile
.
I don't understand the error messages it is giving to me.
1 initialize.c:24:23: error: incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'; take the
address with & [-Werror,-Wint-conversion]
2 initialize.c:21:23: error: format string is not a string literal (potentially insecure) [-Werror,-Wformat-security]
3 initialize.c:21:23: error: format string is not a string literal (potentially insecure) [-Werror,-Wformat-security]
printf(toupper(s[i]));
#include <stdio.h>
#include<cs50.h>
#include<string.h>
#include<ctype.h>
void initialize(string s);
int main(int argc, string argv[])
{
printf("May I have your name?");
string name = GetString();
initialize(name);
}
void initialize(string s)
{
int space = 1;
for (int i = 0;i < strlen(s); i++)
{ if(space == 1)
{
printf(toupper(s[i]));
space -= 1;
}
if(strncmp(s[i]," ",1 ) )
{
space += 1;
}
}
}

printf expects a format string with type const char* as its 1st argument, so:
change
printf(toupper(s[i]));
to
printf("%c", toupper(s[i]));
And as #Matt McNabb pointed, strncmp has the similar problem here. Because you tend to compare the 1st char only, you chould change
if(strncmp(s[i]," ",1 ) )
to
if (s[i] == ' ')
to make it clearer and more effective.

here is the code, after making it portable
But not handling user input errors
It has the necessary corrections so it cleanly compiles
#include <stdio.h>
#include <stdlib.h>
//#include <cs50.h>
#include <string.h>
#include <ctype.h>
void initialize(char * s);
int main( void )
{
printf("May I have your name?");
char name[50] = {'\0'};
fgets(name, sizeof(name), stdin ); // should check returned value
// to assure the line input was successful
initialize(name);
return 0;
} // end function: main
void initialize(char * s)
{
int space = 1;
for (size_t i = 0;i < strlen(s); i++)
{
if(space == 1)
{
printf("%c", toupper(s[i]));
space -= 1;
}
if( ' ' == s[i] ) // always place literal on left so compiler
// catches any place where '=' was used
// when it should have been '=='
{
space += 1;
}
}
} // end function: initialize

Related

Argument type void is incomplete?

My program is supposed to convert a string to lowercase but I keep gettingg this error "Argument type void is incomplete" when trying to convert a string to lowercase and I don't know why. Could anyone explain why this is happening, thanks
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char str[25] ="HELLOWORLD";
void upperToLower(char *str){
for (size_t i = 0; i < strlen(str); ++i) {
printf("%c", tolower((unsigned char) str[i]));
}
}
int main(void) {
upperToLower(str);
printf("%s\n", upperToLower(str));
return 0;
}
OUTPUT
I guess upperToLower is supposed to modify the string in place. Try the following code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char str[25] ="HELLOWORLD";
void upperToLower(char *str){
for (size_t i = 0; i < strlen(str); ++i) {
str[i] = tolower((unsigned char) str[i]);
}
}
int main(void) {
upperToLower(str);
printf("%s\n", str);
return 0;
}
You could also make upperToLower return the pointer that was passed to it (like strcpy does with its first parameter). That allows the the upperToLower call to be done as part of the printf call like this:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char str[25] ="HELLOWORLD";
char *upperToLower(char *str){
for (size_t i = 0; i < strlen(str); ++i) {
str[i] = tolower((unsigned char) str[i]);
}
return str;
}
int main(void) {
printf("%s\n", upperToLower(str));
return 0;
}
The return type of the function upperToLower() is void, so it cannot be used as a value in expressions.
The function upperToLower() prints things inside that, so you won't need to externally print something about that except for the newline character.
Try this:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char str[25] ="HELLOWORLD";
void upperToLower(char *str){
for (size_t i = 0; i < strlen(str); ++i) {
printf("%c", tolower((unsigned char) str[i]));
}
}
int main(void) {
upperToLower(str);
upperToLower(str); /* put this out of printf() */
printf("\n"); /* and print just newline character here */
return 0;
}
Yes. As specified in C specification the type void is an incomplete type.
The void type comprises an empty set of values; it is an incomplete
object type that
cannot be completed.
As result it is not possible to form any value of type void. Pointers of type void* can be used, but they cannot be dereferenced.
Your function returns void, you're literally passing nothing as a parameter.

search of string in an array of strings

i wrote some code that is supposed to find the location of a given string in an array of strings.
problem is- it doesn't give the location. it gives something else.
i understand that probably the problem has to do with the differences between the pointers that are involved- a previous version that dealt with finding the position of a letter in a word worked well.
after a lot of attempts to figure out where is the bug, i ask your help.
kindly, explain me what should be done.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int what (char * token);
main()
{
int i=0;
char string[]="jsr";
char *token;
token=&string[0];
i=what(token);
printf(" location of input is %d \n", i);
return 0;
}
int what (char * token)
{
int i=1;
char *typtbl[]={"mov",
"cmp",
"add",
"sub",
"not",
"clr",
"lea",
};
char * ptr;
ptr=(char *)typtbl;
while (!(strcmp(ptr,token)==0))
{
ptr=(char *)(typtbl+i);
i++;
}
return i;
}
As pointed out, you did not design function what properly. What value should it return if your search function go through all the pointers but does not find the desired string? Typically in that case return -1 would be a choice to indicate nothing found. Also in this case, using a for loop would probably be more suitable, you can just return the index immediately instead of going through all pointers.
int what(char *token)
{
char *typtbl[] = {
"mov",
"cmp",
"add",
"sub",
"not",
"clr",
"lea",
};
for( size_t i = 0; i < sizeof(typtbl)/sizeof(char*); ++i )
{
char *ptr = typtbl[i];
if(strcmp(ptr, token) == 0)
{
return i; // found something
}
}
return -1; // found nothing
}
A cleaner working version.
Main issue is in the (char *)(typtbl+i) replaced by typtbl[i] in the following code. typtbl+i is equivalent to &typtbl[i], so if my memory is good, it's a pointer on the pointer of the string and not the pointer of string itself
I added a NULL at the end of the array to be able to stop if the string is not present and return -1 to clearly say it was not found.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int what(char *token);
int main()
{
int i = 0;
char string[] = "jsr";
i = what(string);
printf(" location of input is %d \n", i);
return 0;
}
int what(char *token)
{
char *typtbl[] = {
"mov",
"cmp",
"add",
"jsr",
"not",
"clr",
"lea",
NULL
};
int i = 0;
while(typtbl[i] && !(strcmp(typtbl[i], token) == 0)) {
++i;
}
if(!typtbl[i])
i = -1;
return i;
}
char *token; token=&string[0]; was useless because string == &string[0].
A few things:
Your main function is missing its return type.
The while loop in what doesn't stop when the element isn't found. Therefore you are reading out of bounds.
This should do the work w/o pointer arithmetic.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int what (char * token);
int main(){
int i=0;
char string[]="jsr";
char *token;
token=&string[0];
i=what(token);
printf(" location of input is %d \n", i);
return 0;
}
int what (char * token){
unsigned int i=0;
char *typtbl[]={"mov",
"cmp",
"add",
"sub",
"not",
"clr",
"lea",
};
unsigned int typtbl_x_size = sizeof(typtbl)/sizeof(typtbl[0]);
char * ptr;
ptr=typtbl[i];
while (!(strcmp(ptr,token)==0)){
i += 1;
if (i >= typtbl_x_size){
printf("element not in list\n");
return -1;
}
ptr=typtbl[i];
}
return i;
}

Segmentation Fault in C

My code is giving me a segmentation fault and I can't seem to find what I'm doing wrong:
#include <stdio.h>
#include <string.h>
char find(char name[], char allNames[][10], int length)
{
int i=0;
for (i = 0; i < length; i++) {
if (strcmp(allNames[i],name) == 1) {
printf("%i",i);
return *name;
}
}
return -1;
}
main(){
char allNames[][10] = {"cat","dog","frog","log","bog"};
char name[] = "log";
int length=5;
printf("%s",find(name,allNames,length));
}
I'm really keen to understand all the mechanisms happening here and what I'm doing wrong for tomorrows exam. Thanks for your help!
EDIT: Really Appreciate the answers and information guys! I'm really quite new to C and just getting used to what every thing means. The particular exam question I am looking at is :
(a) The following function is intended to find the string name in the array
allNames. If found, it returns the position of name in the array. If not
found, it returns -1. Modify the code so that it works correctly.
int find(char name[], char allNames[][10])
{
for (i = 0; i < 10; i++) {
if (allNames[i] == name) {
return name;
}
}
return -1;
}
And I'm trying to get a program to work within these parameters. Cheers :)
http://coliru.stacked-crooked.com/a/d400c9a56d732446
#include <stdio.h>
#include <string.h>
char* find(char name[], char allNames[][10], int length)
{
int i=0;
for (i = 0; i < length; i++) {
if (!strcmp(allNames[i],name)) {
printf("%i",i);
return name;
}
}
return NULL;
}
int main(){
char allNames[][10] = {"cat","dog","frog","log","bog"};
char name[] = "log";
int length=5;
printf("%s",find(name,allNames,length));
}
Returning a single char will do you no good if you're trying to return a string. I would also suggest that you return a NULL if you cannot find the string.
Also, include the int before main; this is better style.
The direct reason for your Segmentation Fault here is because the code tried to print the char type with %s(which needs an address value).
void main()
{
char c = 'a';
printf("%s", c); // will cause Segmentation fault here
}
Back to your code, that is
char find(char name[], char allNames[][10], int length)//return char
printf("%s",find(name,allNames,length));
The minimal change to make it work as follows,
1) To return char*
char* find(char name[], char allNames[][10], int length)//return char*
{
int i=0;
for (i = 0; i < length; i++) {
if (strcmp(allNames[i],name) == 0) { // here should 0
printf("%i",i);
return name; // change name* to name
}
}
return NULL; // change to NULL
}
//to print
printf("%s",find(name,allNames,length));
2) to return position value
int find(char name[], char allNames[][10])
{
for (i = 0; i < 10; i++) {
if (allNames[i] == name) {
return i; // here, change to return i
}
}
return -1;
}
//then, you can print like this
printf("find at position: %d",find(name,allNames,length));
//or to print string by
int pos = find(name,allNames,length);
if(pos >= 0)
printf("find the string: %s",allNames[pos]);
This code is wrong on several levels.
gcc -Wall -Wextra reveals:
meh.c:15:1: warning: return type defaults to ‘int’ [-Wreturn-type]
main(){
^
meh.c: In function ‘main’:
meh.c:19:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
printf("%s",find(name,allNames,length));
^
meh.c:21:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
What's up with that? Do you compile with warnings enabled?
I am ignoring the lack of indentation.
#include <stdio.h>
#include <string.h>
char find(char name[], char allNames[][10], int length)
What? How about: char *name, **list, int size)
{
int i=0;
Why set it to 0 here?
for (i = 0; i < length; i++) {
if (strcmp(allNames[i],name) == 1) {
printf("%i",i);
return *name;
Have you read strcmp's manpage? It returns ZERO when a string matches, so this code makes no sense.
*name is of type char, but you don't want to return a char. You want to return a pointer, no?
}
}
return -1;
Well, given that you feed that into %s in printf, what do you expect to hapen here? Normally one would return NULL.
}
main(){
This is obsolete syntax, I don't know where you stole it from. Use 'int main(void)'.
char allNames[][10] = {"cat","dog","frog","log","bog"};
Normally people just return such arrays with a NULL pointer, so that these can be iterated over and there is no need to pass anything about the size.
char name[] = "log";
Why not char *name = "log".
int length=5;
Incorrect. It hardcodes the amount of stored strings in allNames table.
printf("%s",find(name,allNames,length));
}

Using string as parameter for function

I am trying to use a function with a string as a parameter. I am running into a couple of error messages. First, it says that string[i] is not an array, pointer, or vector, despite the fact that string is a character array. Secondly, it says that I am doing a pointer to integer conversion. Here is my code:
#include <stdio.h>
#include <string.h>
void example (char string) {
int i;
for (i = 0; i < strlen(string); i++) {
printf (string[i]);
}
}
int main (void) {
example("I like pie");
return 0;
}
void example(char string) should be void example(char *string). You declared it to take a character, you want it to take a character pointer or array.
Also, you need to tell printf you are giving it a character: printf("%c", string[i]);.
Your API is wrong it should be
void example (char *string) { // string is a pointer
int i;
size_t n = strlen(string);
for (i = 0; i < n; i++) {
printf ("%c",string[i]); // print character using %c
}
}
Calculate the string length before the loop , calling strlen() in each iteration is not a good idea.
PS: what string points to is read-only you can't modify it
You should use void example(char *string) instead of void example (char string).
#include <stdio.h>
#include <string.h>
void example (char *string) {
int i;
for (i = 0; i < strlen(string); i++) {
printf ("%c",string[i]);
}
printf("\n");
}
int main (void) {
example("I like pie");
return 0;
}
Your function example just receives a character. To get a string you can use a pointer. Also you can use "%s" format specifier in printf instead of using the for loop and strlen() function.
#include <stdio.h>
#include <string.h>
void example (char *string) {
int i;
printf ("%s\n",string);
}

Program does not work (compile problems)

Hello I have a small problem during compilation.
So that problem in my program are:
brackets(str1); - Too few arguments to function call
void brackets(str,len) - Conflicting types for 'brackets'
Ny code -
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void brackets(char str[], int len);
int main()
{
char str[99];
int len;
printf("enter Mathematical exercise: \n");
gets(str);
len = srtlen(str);
brackets(str1);
return(0);
}
void brackets(str,len)
{
char str1[99];
int i,j = 0;
for(i = 0; i < len; i++)
{
if (str[i] == '(')
{
i++;
while(str[i] != ')')
{
str1[j] = str[i];
i++;
j++;
}
}
}
printf("%s\n",str1);
}
I love to know what these errors arise and how can I arrange this program will work. Thanks
Change
void brackets(str,len)
to
void brackets(char str[], int len)
and your function call should be brackets(str, len);. And also change
len = srtlen(str); // Spelling mistake.
to
len = strlen(str);
your function brackets needs two parameters, so you need to give him two:
brackets(str1);
has to become
brackets(str1, len);
You're defining the brackets function as taking 2 arguments but passing only one. Pass the second one as well.
For your second error, if you don't provide a type for the function parameters the compiler will assume it's of type int. As the first parameter have already been declared to be of type char [] there is a mismatch between the declaration (prototype) and the definition of the function.
As for the first error, when you declare a function to take N parameters, you need to call it with N arguments. The exception being functions declare to take a variable number of arguments.
This is your issue: brackets(str1);
You called brackets with just one parameter, yet you define it as this:
void brackets(char str[], int len);
You need to pass an int as a second parameter.
multiple error seems in your code
Starting from main
you not declared str1 in main but used it in brackets(str1);.i think you mean it by str because you declared it char str[99];
so make change by brackets(str);
Also void brackets(str[], len) function not specify data type of function argument please change it to void brackets(char str[], int len)
Also in main len = srtlen(str); the srtlen is not valid but make it len = strlen(str);
And last you call brackets(str1); but brackets expect two argument so please make change in main by brackets(str,len);
After above change your code is now clean without error as follows
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void brackets(char str[], int len);
int main()
{
char str[99];
int len;
printf("enter Mathematical exercise: \n");
gets(str);
len = strlen(str);
brackets(str,len);
return(0);
}
void brackets(char str[],int len)
{
char str1[99];
int i,j = 0;
for(i = 0; i < len; i++)
{
if (str[i] == '(')
{
i++;
while(str[i] != ')')
{
str1[j] = str[i];
i++;
j++;
}
}
}
printf("%s\n",str1);
}

Resources