I'm trying to acquaint myself with the atoi function, so I wrote a basic programme using it, but I'm having some problems:
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
int main(void)
{
string s = get_string("String:");
int i = get_int("Integer:");
int a = atoi(s[1]);
int j = i + a;
printf("%i\n", j);
}
When I try to compile it, I get the error message "incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'; take the address with & [-Werror,-Wint-conversion]". This seems to suggest that it wants something to do with a char, but from what I've read, I was under the impression that atoi was used with strings. If someone could explain where I'm going wrong, I'd be very thankful
You're passing a char(assuming string is a typedef for char*) by indexing the string and it wants you to pass a char*. So just pass the full string:
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
int main(void)
{
string s = get_string("String:");
int i = get_int("Integer:");
int a = atoi(s);
int j = i + a;
printf("%i\n", j);
}
here is the syntax for atoi()
int atoi(const char *nptr);
Notice the parameter is a pointer, not a single character.
However, the posted code has:
int a = atoi(s[1]);
which is a single character, not a pointer
suggest:
int a = atoi( s );
Also, the function: atoi() has no facility to let the program know when an error occurred. Suggest using the strtol() function which does have a facility to indicate when an error occurred
Related
i noticed that variable value have been changed after strcpy function,i don't know how and why this is happening.
this is my code:
#include <stdio.h>
#include <string.h>
int main()
{
int b = true;
char ch[3];
strcpy(ch,"int");
printf("b value is:%d\n",b);
return 0;
}
I even use a temporary integer variable. it's very strange , in this case, the b value is correct after strcpy but the temp is changed to 0, note that it's just happened when assigning 1 value to b and temp variables.
this is the second code
#include <stdio.h>
#include <string.h>
int main()
{
int b = true;
int tmp=b;
char ch[3];
strcpy(ch,"int");
printf("b value is:%d\n",b);
return 0;
}
strcpy(ch,"int");
is equivalent to
ch[0] = 'i';
ch[1] = 'n';
ch[2] = 't';
ch[3] = 0;
Seeing as ch only has three elements (ch[0]..ch[2]), this invokes undefined behaviour.
Firstly, you need to use number (0,1) in your boolean variables. If you want to use boolean in C as you use them in c++, you have to include stdbool.h library.
Concerning strcpy(dest, src) function, it copies the src string to dest and return à pointer to the copied string.
In reality the size of "int" is not 3 but 4 bytes. String always ends with '\0' character.
In other words :
If you don't understand, try to analyze this code :
#include <stdio.h>
#include <string.h>
int main()
{
int b = 1;
char ch[4];
strcpy(ch,"int");
printf("b value is:%d\n",b);
return 0;
}
I used online compiler and selected "C++" and the error is not happening.
but when I selected C++17 I got this warning "
input
main.cpp:9:12: warning: ‘void __builtin_memcpy(void*, const void*, long unsigned int)’ writing 4 bytes into a region of size 3 ov
w=] "*
as #PaulMcKenzie mentioned this is buffer overflow case.
I am trying to create a hexadecimal to base64 converter. I don't know if I am going in the right direction converting binary as I am attempting to do now or if there is a more direct way of converting. Any suggestions on the math of converting or how to code hex to base64 would be very helpful.
On the contrary, I have been receiving the error(The new one since updated code):
hexto64.c: In function ‘main’:
hexto64.c:21:17: error: lvalue required as left operand of assignment
ReVerse(input) = RevHex;
^
If anybody could help explain what this error means and how to fix
would help greatly! Thank you in advance.
EDIT: So thanks to the few people in the comments, I now understand the error.
Here is my code(Updated):
#include <stdio.h>
#include <math.h>
#include <string.h>
char ReVerse(const char *str)
{
if (*str != '\0')
ReVerse((str + 1));
printf("%c", *str);
}
int main()
{
char RevHex;
char input[4096] = {0};
printf("Enter Hexadecimal: ");
scanf("%s", input);
RevHex = ReVerse(input);
printf("\n");
return 0;
}
Last edit: I have found the error in my code. Thank you guys for all the feedback!
You try to use the return value of the function ReVerse() but this function return void. It's "nothing" so you can't assign "nothing" to something. Here you try to put your array RevHex to "nothing". This don't make sense.
If your function is just reversing the entered Hexadecimal string then you can do it like this:
#include <stdio.h>
#include <math.h>
#include <string.h>
void ReVerse(const char *str) {
if (*str != '\0')
ReVerse((str + 1));
printf("%c", *str); }
int main() {
char input[10] = {0};
printf("Enter Hexadecimal: ");
scanf("%s", input);
ReVerse(input);
printf("\n");
return 0; }
Here I do not think that this char RevHex[4096] = {0}; is useful. Because you want to reverse the input string. If you implement it like this then this program will work.
Talking about your program, you are assigning a string value to a void function, that is why it is giving the error.
I don't know how to "fix" this but trying to assign RevHex to a function will throw an error.
Given that ReVerse doesn't return anything, I am not sure what RevHex is for...
The error is this line: ReVerse(input) = RevHex;
You can't assign something to a call of void function, you should pass it to the function as parameter.
I've looked around everywhere and tried pretty much everything suggested and can't get anything to work.
this is my code:
#include <stdio.h>
#include <string.h>
#include <math.h>
int main(){
float a;
char *nums[3];
char str[5];
printf("Please enter a,b,c:");
scanf("%s",str);
int i=0;
char *p;
p = strtok (str,",");
while (p != NULL)
{
nums[i++] = p;
p = strtok (NULL, ",");
}
a=atof(nums[0]);
printf("%s\n",nums[0]);
printf("%f\n",a);
return 0;
}
the math.h is for something later on after I figure this out. So if I entered "1,2,3" into this program, my print statements would show me "1" and then "0.000", this is obviously just there for me to test things out but why the does my value just disappear after trying to convert to a float? I need that value of 1 to do math with later in my program but I can't get that char pointer value no matter what I try, I can only seem to print it, but it screws up as soon as I try to convert it into a type I can use.
Two issues:
You nums and str arrays are too short. nums should have a size of at least 3, and str should be at least 6 ("1,2,3" plus null byte), probably more for larger numbers.
So change those to:
char *nums[3];
char str[20];
Second, you don't #include <stdlib.h>, which contains the declaration of atof. Without a declaration, it is assumed to return an int.
Fix the array sizes, and #include <stdlib.h>, and it should work.
I am pretty new to programming on Linux. I am trying to implement a message queue in one of my assignments. But I am not able to do it. The code is as follows :
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <linux/sched.h>
#include <stdlib.h>
#include <string.h>
typedef long MSISDN;
typedef struct
{
long mtype;
long mtext;
}msgbuf;
void init(int qid,int key) {
qid = msgget(key,IPC_CREAT|0666);
}
void sendMsg(long t_ype, long buf, int len, int qid) {
int length = sizeof(long) + sizeof(MSISDN);
msgbuf *p = malloc(length);
p->mtype = t_ype;
fputc('2',stderr);
void* tosend = (void*) buff;
fputc('3',stderr);
memcpy(p->mtext,tosend,length);
fputc('4',stderr);
msgsnd(qid,p,length,IPC_NOWAIT);
free(p);
}
void main()
{
int qid;
int key = 1111;
int len= sizeof(MSISDN);
long type_1=1;
long send = 12345;
init(qid,key);
fputc('1',stderr);
sendMsg(type_1,send,len,qid);
getchar();
}
The problem is memcpy is not working . I am getting a warning :
. warning: passing argument 1 of ‘memcpy’ makes pointer from integer without a cast [enabled by default]
Also when I run the code it gets a SIGSEGV signal at the memcpy. I think I am not getting the typecast correctly.
It's not the typecast, it's the argument itself. p->mtext is a long, not a pointer. You need to send the address of p->mtext as the dest argument to memcpy. You're getting the segfault because memcpy is trying to write to the memory address pointed to by p->mtext, which is clearly not in your process' address space.
That's the reason - since this is a homework assignment, I'll leave the fixing of the code up to you.
You would explicitly type cast a variable like this
double num = 1.11;
int num2 = (int)num;
num = 1.11
num2 = 1
I'm trying to print all characters stored in hex array to the screen, one by one, but I get this strange error in line 16. As far as I know, %c should be expecting a char, not an int.
Why I'm getting this error?
Below is my code, thanks.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <ctype.h>
#include <string.h>
int main()
{
char hex[8] = "cf0a441f";
int hexCounter;
char *currentHex;
for(hexCounter=0; hexCounter<strlen(hex); hexCounter++)
{
currentHex = &hex[hexCounter];
printf("%c",currentHex);
}
return 0;
}
You mean
printf("%c", *currentHex);
In my opinion you can remove the entire idea of currentHex since it just adds complexity for no value. Simply do:
printf("%c", hex[hexCounter]);
The important point is that you're supposed to pass the value of the character itself, not it's address which is what you're doing.
You have hex[hexCounter] as a char so when you set
currentHex = &hex[hexCounter];
you are setting currentHex to the address of a char, i.e. a char *. As such, in your printf you need
printf("%c",*currentHex);
What you are doing is unnecessary anyway, since you could just do
printf("%c",hex[hexCounter]);
currentHex should be of type char, not char *.
char currentHex;
[..]
currentHex = hex[hexCounter];
printf("%c",currentHex);
If you really want it to be a pointer, dereference it to print:
printf("%c",*currentHex);
Here's the modified code which runs fine for me -
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <ctype.h>
#include <string.h>
int main()
{
char hex[9] = "cf0a441f";
unsigned int hexCounter;
char *currentHex;
for(hexCounter=0; hexCounter<strlen(hex); hexCounter++)
{
currentHex = &hex[hexCounter];
printf("%c",*currentHex);
}
return 0;
}