Ask your own question, for FREE!
Computer Science 14 Online
OpenStudy (davidusa):

C Programming Help What does this error message mean and how do I fix it?

OpenStudy (davidusa):

@saifoo.khan @e.mccormick

OpenStudy (davidusa):

One sec I'm posting it..

OpenStudy (davidusa):

#include <stdio.h> #include <cs50.h> #include <ctype.h> #include <stdlib.h> #include <string.h> int main(int words, string letters[]) { if(words < 2) { printf("Usage: ./caesar <phrases>\n"); return 0; } else { printf("What is the key?"); int key = GetInt(); for(int x = 1; x < words-2; ++x) { for (int y = 0; y < strlen(letters) - 1; ++y) { printf("%d", key); } } } } Here is my code.

OpenStudy (davidusa):

jharvard@appliance (~/pset2): make caesar clang -ggdb3 -O0 -std=c99 -Wall -Werror caesar.c -lcs50 -lm -o caesar caesar.c:20:40: error: incompatible pointer types passing 'string *' (aka 'char **') to parameter of type 'const char *'; dereference with * [-Werror,-Wincompatible-pointer-types] for (int y = 0; y < strlen(letters) - 1; ++y) ^~~~~~~ * /usr/include/string.h:399:35: note: passing argument to parameter '__s' here extern size_t strlen (const char *__s) ^ 1 error generated. make: *** [caesar] Error 1 And here is the error message. Should I take a screenshot or is this good? @saifoo.khan

OpenStudy (davidusa):

@confluxepic Can you help?

OpenStudy (davidusa):

@ikram002p @CausticSyndicalist @rvc @paki

OpenStudy (davidusa):

@sleepyjess :)

OpenStudy (sleepyjess):

@Jaynator495

OpenStudy (davidusa):

HELLO!!!

OpenStudy (davidusa):

Please help this poor fellow

jaynator495 (jaynator495):

>_<

jaynator495 (jaynator495):

One momment ill read it... *Gives evil glance over at sleepyjess*

OpenStudy (davidusa):

Lol

OpenStudy (davidusa):

Should I take a screenshot?

jaynator495 (jaynator495):

Before I read it, What programming language is it so i know if i can help or not XD

OpenStudy (davidusa):

Read the question xD- It's in C

OpenStudy (davidusa):

At the top lol

jaynator495 (jaynator495):

Theres a language called "C" ._. Learn somethign new every day XD

OpenStudy (davidusa):

Ya, and C++

jaynator495 (jaynator495):

I know of c++ ._.

OpenStudy (davidusa):

So you can'tz Helpz mez? meowww

jaynator495 (jaynator495):

Let me see if I can spot someone that can though xD

OpenStudy (davidusa):

where's the ambassador help page

OpenStudy (saifoo.khan):

strlen = You want to determine the length of the array, right?

OpenStudy (davidusa):

No it is to determine the length of the string that is in the parentheses- called letters

OpenStudy (saifoo.khan):

Length of string on which index of array? :P

OpenStudy (davidusa):

Ummm.. Idk. By the way you know C programming right?

OpenStudy (davidusa):

Here's a video that can help... http://cs50.tv/2012/fall/shorts/command_line_arguments/command_line_arguments-720p.mp4

OpenStudy (davidusa):

I've never seen this error message before and I can't find anything wrong..

OpenStudy (saifoo.khan):

See, when you want to check something in an array (even if you want to check the length of the string inside the index[0]), you still need to give it the index of the array where you want to apply strlen().

OpenStudy (davidusa):

Sorry i was watching the video.

OpenStudy (saifoo.khan):

I see what the problem is. You're passing a string array. Whereas you should be passing a char* agrv[].

OpenStudy (saifoo.khan):

As char* argv is a pointer type array.

OpenStudy (davidusa):

Hmmm... I just started learning C I don't understand Sorry :C

OpenStudy (saifoo.khan):

Umm, don't know about pointers? Don't worry, you'll learn them at a later stage. For now, just change it to char* argv[].

OpenStudy (davidusa):

umok

OpenStudy (davidusa):

wait what should i change to char* argv[]

OpenStudy (anonymous):

change strlen(letters) to strlen(*letters)

OpenStudy (anonymous):

string is of type 'point to a pointer of a char'. strlen method expects type 'pointer to a char'. So you need to 'derefence' the letters variable. Hence the operator * in front of it.

OpenStudy (davidusa):

@confluxepic why dont u accept messages?

OpenStudy (confluxepic):

I changed my settings now. @DavidUsa

OpenStudy (davidusa):

@e.mccormick Please help, I still need it :)

OpenStudy (e.mccormick):

What issue are you having now? Stillt he same one or something else?

OpenStudy (davidusa):

Something else. I'll post now so I can give you medal :)

OpenStudy (anonymous):

Calculate strlen once, store it, then use it as a condition. Don't use a linear \(O(n)\) solution to a constant \(O(1)\) problem.

OpenStudy (anonymous):

The problem here is that `letters` is an array of `string`s. You only want to pass in a single `string`. For example, if they ran the command ``` ./caesar I am the law. ``` Then we'd have data similar to: ``` letters[0] = "I"; letters[1] = "am"; letters[2] = "the"; letters[3] = "law."; words = 4; ``` First you want to iterate through your words. Then you can iterate through each letter of each word. By the way `*letters` is no different that `letters[0]`.

OpenStudy (davidusa):

@wio why are you replying to a closed quesition? :P

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!