Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am writing a program that takes command line arguments and performs basic arithmetic operations on them. I am using getopt to get the arguments and I am storing them as strings. Below are the variables I store the arguments in
char *distance = NULL;
char *time = NULL;
char *pace = NULL;
However how do I then convert them to decimals? So for example "5" will become 5.00 or "6.12" will become 6.12. I have tried search around but other solutions don't seem to work for me.
I have tried doing
double testnum;
testnum = atof(time);
but I get
error: request for member 'testnum' in something not a structure or a union
You can also try strtof()
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char *string, *stopstring;
double x;
float f;
long double ld;
string = "3.1415926This stopped it";
f = strtof(string, &stopstring);
printf("string = %s\n", string);
printf("strtof = %f\n", f);
printf("Stopped scan at \"%s\"\n\n", stopstring);
}
Just use atof:
double atof (const char* str);
Parses the C string str, interpreting its content as a floating point number and returns its value as a double.
It's not particularly robust though: in particular it returns zero if the input is not convertible, and undefined behaviour if the input number is too big.
Take a look at the atof function. it takes a char * and tries to parse a double.
You can start off by scanf with the appropriate type.
scanf("%f", &time);
and then use printf to convert a type to a string when necessary.
If you do need to convert types from string, look for an atoX style function to do the conversion. For example, here is a program that converts a (ascii) to i (integer)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int val;
char str[20];
strcpy(str, "98993489");
val = atoi(str);
printf("String value = %s, Int value = %d\n", str, val);
strcpy(str, "tutorialspoint.com");
val = atoi(str);
printf("String value = %s, Int value = %d\n", str, val);
return(0);
}enter code here
The example code you provide does not match the error you have. The error tells us that testnum is not found in a structure.
I'm guessing you have a pointer to the structure and use non-pointer . to access the member. Or that you have a non-pointer structure and use pointer -> to access the member.
Related
My code:
#include <stdio.h>
int main() {
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE ='\n';
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
return 0;
}
In the above code the output:
value of area: 50
Process returned 0 (0x0) execution time : 2.909 s
Press any key to continue.
There is a new line inserted, but when I change NEWLINE="\n" despite knowing it is a char type, there is no error prompted by the compiler and no newline printed out. Why???
Also, I modified my code as,
#include <stdio.h>
int main() {
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE ='\n';
const char k="hjk";
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
printf("%c", k);
return 0;
}
The output is only the area calculated and the new line but k is not printed out. I also find this very weird! Can you please give suggestions?
Please be kind enough with the suggestions and point out my mistakes because I am a beginner at C.
The problem is that you are trying to save a string as a char, so you have to change const char k = "hjk" to const char k[]="hjk" and print it using %s instead of %c.
#include<stdio.h>
int main() {
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE ='\n';
const char k[]="hjk";
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
printf("%s", k);
return 0;
}
Some clarification: if you save a "string" without specifying that it is an array of characters char[], if you try to print it as a char %c a warning would be generater (warning: incompatible pointer to integer conversion initializing 'const char' with an expression of type 'char [4]') and if you try to print it as a array of characters %s (string) you are going to receive a segmentation fault.
when I change NEWLINE="\n" despite knowing it is a char type, there is no error prompted by the compiler
const char NEWLINE = "\n"; is invalid C. The reason why it is invalid is explained in detail here: "Pointer from integer/integer from pointer without a cast" issues
The compiler is not required to produce an "error", but it is required to produce some sort of diagnostic message. See What must a C compiler do when it finds an error?
Why your compiler decided to spew out a binary regardless of getting fed invalid C is anyone's guess. You have to ask the people who made the compiler. In case of gcc, you won't find an answer, because this is completely undocumented behavior.
And therefore, any output you get from such a "non C" program is also completely non-deterministic, unless a compiler documented the behavior among non-standard compiler extensions. gcc did not.
Similarly, const char k="hjk"; is also invalid C.
k seems an array of char.
Try to use:
const char k[] = "something";
printf("%s", k);
The statement const char k="hjk"; is not valid C code. Apparently, your compiler accepts it and assigns the memory address where the literal string "hjk" begins to k. Both a memory address and a char are implemented as integer numbers, so the memory address is interpreted as the numeric code for a character.
Because k is now, most likely, an unprintable character, printf("%c", k); will print nothing.
Exactly the same happens when you do const char NEWLINE ='\n';.
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'm trying to printf an integer that was passed by command line but the console print a long random values.
I put this into RUN "C:\Users\pc\Documents\Visual Studio 2013\Projects\Lab3\Debug\Lab3.exe randomString 4"
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[]){
printf("%s\n", argv[0]); // Working
printf("%s\n", (argv[1])); // working
printf("%d\n", (int)(argv[2])); // NOT Working
printf("%d\n", argc); // Working
system("pause");
return 0;
}
You can't just "cast" a char* pointer to an int and expect it to be magically converted. That is just printing the address of the first element of the array. You need to convert it to an int with a runtime function such as atoi(argv[2]) See function details here
The argv[2] variable is a string pointer (char* to be precise). So casting it to int will just give you the numerical value of this pointer (or part of it, depending on the size of the addresses on your system). And this is exactly your "random long number" that you are seeing. In order to convert the string to a number you can use functions like atoi.
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>
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
assignment makes pointer from integer without a cast
assignment makes pointer from integer without a cast
I'm currently learning C and struggling a bit. I'm making a program that loops through an array of questions and populates an array of answers. I keep getting the following warning:
/var/Cprograms/quiz.c|24|warning: assignment makes pointer from integer without a cast [enabled by default]|
I'm still trying to grasp the use of pointers and I may be using them completely wrong but that's why I'm here to learn. So I need help figuring what I'm doing wrong on line 24 in order to get the program working and eventually get it to print out the answers. Here's the code so far
#include <stdio.h>
/*
Yes or no Question Quiz:
get answers from command prompt
and print answers at end of session
using pointers
*/
/* char *gets(char *s) */
void loopQuiz (void)
{
char *questions[] = {"Is true == true?", "Is 0 == 1?", "Does water have atleast 3 phases?", "Is C a programming language?"};
char *answers[3];
int i = 0;
int count = sizeof(questions) / sizeof(int);
do
{
printf("%s \n", questions[i]);
answers[i] = getchar();
i++;
}
while (i < count);
/*print function to go here*/
}
int main (void)
{
loopQuiz();
return 0;
}
char *answers[3];
answers is an array of 3 pointer to char.
answers[i] = getchar();
Attempts to assign an int to answers[i]. Your types are wrong. getchar returns an unsigned char converted to an int (unless you hit EOF), so what you want is...
char answers[3];
(You owe me a donation for counting until the 24th line in your code...)
The problem is on this line:
answers[i] = getchar();
Since answers is an array of char *, but you assign an int (the return value of getchar()) to one of its members, this is an invalid assignment. I don't see what you expect this to do, but it is certainly wrong. You need to assign a char * to answers[i].
answers[i] = getchar();
getchar() returns an int, but
char *answers[3];
answers is declared as an array of pointers, so you're assigning an integer to a pointer without casting, just as the error message says.