Hot answers tagged

8

Type casting using (int) would work predictably on a single char, although it would probably not give you the result you are expecting anyway. It does not give anything meaningful on a string. A string is just an array of chars where the variable name is a pointer (or memory address) to the first element in that array. In your first example, you are trying ...


3

First, understand what strings in C are. Strings in C are pointers to a character, or char* (if you use type string, that's a custom alias for the same thing). The convention then says that the pointer points the first byte of a string (this time in the sense of the actual characters), and the string ends at the first character that's zero, often called "...


2

If this is for example for Caesar cipher and you're running ./caesar 2 then key should indeed be set to an integer of 2 as argv[0] is the program name and argv[1] is the string 2. But int atoi(message); first doesn't set the return value of atoi to anything, no variable, and also if message is a string of chars, which from it's name it seems to be, then ...


1

You are meant to reject any number of command-line arguments other than one (means argc should be 2). To test whether a given character is numeric/alphabetic, you can use the isdigit/isalpha function (actually implemented as a macro, so mistakes in code might show up in weird ways), like if (isalpha(argv[1][j])). Or, test a range, like if (argv[1][j] >= '...


1

Is this due to where I am putting my atoi code? actually it's because you didn't read atoi's manual page. carefully, at least. the man page explicitly states that atoi does not detect errors which means that it returns some integer value normally even if an error occurs (e.g., the passed string does not start with a number). this returned integer value ...


Only top voted, non community-wiki answers of a minimum length are eligible