Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am trying to create a function that will add the total of 3 exams, divide them by 4 and then multiply them by .4 and pass the resulting number back to a pointer. I'm not sure which line number is the issue or what I am missing here.
I'm new to coding in C and new to Stack Overflow.
void calcExams(void)
{
int i;
float examTotal;
float *calcExams;
float oneExam;
for (i = 0; i > 3;)
{
printf("\n Enter an exam grade: ");
scanf("%f", oneExam);
examTotal = examTotal + oneExam;
*calcExams = examTotal / 4 * .4;
}
}
Problem 1:
A pointer, as the name hints at, should point somewhere, i.e to some valid memory location, as you have it, it's uninitialized, it points nowhere, or more accurately, to some random memory location given by whatever residual value is stored in it when it's declared, accessing such memory location by dereferencing the pointer amounts to undefined behavior.
That is to say your pointer must be initialized before it's used, either by way of memory allocation or otherwise making it point to some valid variable:
#include <stdlib.h>
//...
float *calcExams = malloc(sizeof *calcExams);
Or:
float some_variable;
float *calcExams = &some_variable;
Problem 2:
scanf expects as an argument the address of the variable on which to store the inputed value, but you are passing this variable by value, you need:
scanf("%f", &oneExam);
^
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
i wrote code, and it gives me answers like 0.00 and 1.00, not actual math answer. Where i made mistake? (im begginer, dont scream on me :) )
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b;
float x;// score
// Request 1
printf("Zadanie 1(12)\n");
// Calculate the square root of the given interval
printf("Oblicz pierwiastek rownania w podanym przedziale\n");
// Give me a number a
printf("Podaj liczbe a:\n");
scanf("%d",&a);
// Give me a number b
printf("Podaj liczbe b:\n");
scanf("%d", &b);
x=&a-&b;
//answer
printf("%f", x);
system("PAUSE");
return 0;
}
x = a - b
not
x = &a - &b
Explanation: The & operator gives you the memory address of a, which you need to give to scanf, so that it can place data there. But you do math on the actual value of a.. which is just a.
because of pointer arithmetic:
x = &a - &b;
computes the distance between the addresses of a and b, which are probably close since they're auto variables of the same type declared close-by. My guess is that you could get 1 or -1 (or any other integer value but not 0 since a and b are located in 2 separate addresses) then put in a float.
(the difference of addresses of 2 consecutive integers is 1, independently of the size of the integer)
You need of course to do:
x = a - b;
You've been misled by the fact that scanf needs a pointer on a or b to be able to write a value when reading the input.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am new to C. I was just interested in making a program which takes two integer input from the user and add it but the condition is that i have to use only one variable. I came up with this code:
#include <stdio.h>
int main()
{
int *a;
scanf("%d%d",a,(a+1));
printf("\nSum=%d",*a+*(a+1));
return 0;
}
scanf() function takes an valid address as an argument and i am passing the value in a(which is not initialised yet). So, how this code worked in Turbo C++?
You are trying to access an area that is not within the scope of the program. Luckily TCC gave it, but I believe if you go on experimenting, results will be undefined.
You can do something like this to solve your problem of adding using 1 variable.
int main()
{
int a;
scanf("%d",&a); //scan the first number
getchar();
a += getchar()-'0'; // get the second number (restricted to 0-9)
printf("%d",a);
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
The above expression will breakdown like *(&(&(&(*(*(*X)))))
As far as I could solve I think the compiler can trace the value at (***X). But what happens when it tries to retrieve the address of the value i.e'; &(***X). Will this work? Because the value is some random number is an expression. Will the compiler search for the value and return its address? If not what will be the output?
A compiler error. You can't dereference an int.
Your assumption is incorrect: *&&&***x will not break down like *(&(&(&(*(*(*X))))), it will be tokenized as * && & * * * x, resulting in a syntax error.
Adding spaces as in *& & &***x will result in a constraint violation because you cannot take the address of an address, nor can you dereference the int variable x as a pointer.
Conversely, *&*&*&*&x is a correct albeit contorted way to take the value of x, and so is 0[&x].
no, the problem occurs since x is of type int. it contains the value 2. when you say *x, it means "I want the value of the variable that x points to." and you know 2 is not a valid address in memory. this cause error.(the only valid address that you can assign is 0.)
also, since x is of type int, you can't dereference int.you will got a compile error.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
is there any way how to put integer sized 100 000 into 4 elements of char array? If I use sprintf or itoa, array has 6 elements. I tried to use this, but it didnt work. And is there any way how to put these 4 elements back to integer?
char *s;
int value = 100000;
*((int *)s)=value;
Note that:
int value = 100000;
char *s;
*((int *)s)=value;
dereferences uninitialized pointer s, which causes undefined behavior. You could do:
int value = 100000;
char s[4];
*((int *)&s[0])=value;
just note that this stores value in the memory block "occupied" by charr array (at memory level) unlike sprintf, which would print the value in a form of string (characters representing the number).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I use a function that displays a block of memory's content pointed by a pointer.
But didn't get the desired output, i am new to this, please correct my if i am wrong.
when I input size =3, element = 1,2,3 , I got output = 1 only.
Here's the code:
#include <stdio.h>
#include <stdlib.h>
void merge(int **arr1);
int main(void) {
int size1;
printf("Give me the size of first array\n");
scanf("%d", &size1);
int *arr1 = malloc(size1*sizeof(int));
int *p1=arr1;
printf("Give me the elements of first array\n");
int index1;
for(index1 = 0 ; index1<size1; index1++)
scanf("%d", p1++);
merge(&arr1);
return;
}
void merge(int **arr1) {
while(**arr1) //**arr1 is the content of the passed array, if there
// is an int in it, print that out and increment to next one
{
printf("%d", **arr1); // ** is the content and * is the address i think, right?
*arr1++;
}
}
Your merge() code expects the array to be terminated by zero. The calling code is not doing it, so the behavior is unspecified (I got segfault when I tried your code).
The other issue is that you should put parentheses around *arr1:
(*arr1)++;
When I run your code with this modification and enter zero for the last element, your code runs fine.