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.
Related
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
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'm new to the world of C programming, and I as trying to code a primitive, terminal-based version of the "Hangman" game.
One of the steps doing this (or at least the way I am working on), is to create a second char array (next to the original char array that stores the word one needs to guess), filled with "*" for every Char of the original array, and display it. Although the rest of the programming part is not there yet (since I am not finished with it yet), I doubt it is relevant for now (however I allready know how to proceed, that is if I weren't bothered by some error-messages)....
Here's the code I have so far:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
void hiddenArray(char array[]);
char *secretWord;
char *arrayCover;
char letter;
int main(int argc, char *argv[]){
secretWord = "Test";
printf("Welcome to the Hangman!\n\n");
hiddenArray(secretWord);
printf("What is the hidden Word?: %c\n", *arrayCover);
printf("Guess a letter\n");
scanf("%c", &letter);
}
void hiddenArray(char array[]){
int size,i;
size = sizeof(*array);
for (i=0;i<size-1;i++){
*(arrayCover+i) = "*";
}
}
Now I have two issues... the first one:
I don't understand the error message I am getting after compilation:
pendu.c:41:19: warning: incompatible pointer to integer conversion assigning to 'char' from 'char [2]' [-Wint-conversion]
*(arrayCover+i) = "*";
^ ~~~
1 warning generated.
And my second question: the second Array created, filled with "*" is not being displayed, what did I do wrong?
I'd love for some help, cheers!
Your program have some errors to be corrected .
1) Your *arraycover is pointing to some unknown value, You have not initialized it.
2) sizeof(*array) should be sizeof(array)
3) *(arrayCover+i) = "*" should be *(arrayCover+i) = '*';
I suggest you not to create too many global variables when you dont really need them
Create char secretWord[100] = "Test" instead of char *secretWord
Try this
size = sizeof(arrayCover)/sizeof(char);
for (i=0;i<size-1;i++){
*(array+i) = '*';
}
Despite of all these, I think you need to allocate memory for arrayCover
arrayCover = malloc(sizeof(char) * number_of_elements);
"*" is a string. Use '*' instead to get the char.
"*" is a string which also contains \0 also, resulting to 2 characters. Instead use '*' which is just a character.
The function hiddenArray has the formal argument array which need to be used locally instead of arrayCover as below,
void hiddenArray(char array[]){
int size,i;
size = sizeof(array)/sizeof(array[0]);
for (i=0;i<size-1;i++){
*(array+i) = '*'; /* Or array[i] = '*' */
}
}
I am tring to convert a string into a floating-point value. Take a look at my small program:
#include <stdio.h>
int main() {
char string[3] = "42";
double value = atof(string);
printf("Floating-point value: %f\n", value);
return 0;
}
When I run it, I get this:
Floating-point value: 327680.000000
Why? The conversion from string to integer using atoi has worked very well!
If you have any idea why this is, please share your wisdom. :)
char string[2] = "42";
should be
char string[3] = "42";
the size of "42" array is 3 bytes as you have to count the trailing null character. If you want use char string[2] for the declaration, your string will not be null terminated.
Then you also have to include stdlib.h file for atof declaration:
#incude <stdlib.h>
I just started to look at C, coming from a java background. I'm having a difficult time wrapping my head around pointers. In theory I feel like I get it but as soon as I try to use them or follow a program that's using them I get lost pretty quickly. I was trying to follow a string concat exercise but it wasnt working so I stripped it down to some basic pointer practice. It complies with a warning conflicting types for strcat function and when I run it, crashes completly.
Thanks for any help
#include <stdio.h>
#include <stdlib.h>
/* strcat: concatenate t to end of s; s must be big enough */
void strcat(char *string, char *attach);
int main(){
char one[10]="test";
char two[10]="co";
char *s;
char *t;
s=one;
t=two;
strcat(s,t);
}
void strcat(char *s, char *t) {
printf("%s",*s);
}
Your printf() should look like this:
printf("%s",s);
The asterisk is unnecessary. The %s format argument means that the argument should be a char*, which is what s is. Prefixing s with * does an extra invalid indirection.
You get the warning about conflicting types because strchr is a standard library routine, which should have this signature:
char * strcat ( char * destination, const char * source );
Yours has a different return type. You should probably rename yours to mystrchr or something else to avoid the conflict with the standard library (you may get linker errors if you use the same name).
Change
printf("%s",*s);
to
printf("%s",s);
The reason for this is printf is expecting a replacement for %s to be a pointer. It will dereference it internally to get the value.
Since you declared s as a char pointer (char *s), the type of s in your function will be just that, a pointer to a char. So you can just pass that pointer directly into printf.
In C, when you dereference a pointer, you get the value pointed to by the pointer. In this case, you get the first character pointed to by s. The correct usage should be:
printf( "%s", s );
BTW, strcat is a standard function that returns a pointer to a character array. Why make your own?
Replacing *s with s won't append strings yet, here is fully working code :
Pay attention to function urstrcat
#include <stdio.h>
#include <stdlib.h>
/* urstrcat: concatenate t to end of s; s must be big enough */
void urstrcat(char *string, char *attach);
int main(){
char one[10]="test";
char two[10]="co";
char *s;
char *t;
s=one;
t=two;
urstrcat(s,t);
return 0;
}
void urstrcat(char *s, char *t) {
printf("%s%s",s,t);
}
pointers are variable which points to address of a variable.
#include "stdio.h"
void main(){
int a,*b;
a=10;
b=&a;
printf("%d",b);
}
in the follwing code you will see a int 'a' and a pointer 'b'.
here b is taken as pointer of an integer and declared by giving'' before it.'' declare that 'b' is an pointer.then you will see "b=&a".this means b is taking address of integer "a" which is keeping value 10 in that particular memory and printf is printing that value.