My function isn't working properly. The program runs but when it gets to the for loop the function doesn't work and stops the program even though it is supposed to continue looping. If you could please check my Array function and tell me if there is anything i'm not understanding or doing correctly.
Thanks for your time.
I know for a fact the loop isn't the problem because when I remove the function it works fine. I've also tried placing the 'b' within the function array parameter like this "int Array(int a[b], int b, int c);"
#include <stdio.h>
#include <stdlib.h>
/*Function*/
int Array(int a[], int b, int c);
/*Main Program*/
int main()
{
int S, C, *A, *B;
printf("How Many Numbers Would You Like in Array A and B? ");
scanf("%d\n", & S);
/*For Loop Asking The User to Enter a Value and using the Array function to calculate/store the B[] Value*/
for (C=0; C<=S; ++C){
printf("\nWhat is A[%d] ", C);
scanf("%d", & A[C]);
B[C] = Array(A, S, C);
}
}
/*Function*/
int Array(int a[], int b, int c)
{
if (a[c] < 0){
return a[c] * 10;
} else {
return a[c] * 2;
}
}
Expected Results:
The program asks the user to input the array size which will be used for *A and *B
The program uses a for loop to ask the user to enter a value for each position in array *A, using that value to compute the value for each matching B position
Actual Results:
The program asks the user to input the array size which will be used for *A and *B
The program uses a for loop to ask the user to enter a value for each position in array *A, the program asks the user for one value then stops running.
You're not allocating any memory for the array A. You just declare it as a pointer to int, then start writing values to it, which are going to some random memory location. After the first scanf that gets S, you need to assign A = malloc(S * sizeof(int)) before accessing it.
Related
I just started learning, I didn't understand the book, so I asked for advice.
I am a beginner and don't have a good English.
Function: Combine two two-digit positive integers A and B to form an integer in C
Middle. The method of merging is: the ten digits and single digits of the A number are placed on the thousand and ten digits of the C number, and the ten and single digits of the B number are placed on the single and hundred digits of the C number.
For example: when a=45, b=12. After calling this function, c=4251.
Here is my code
#include <stdio.h>
void fun(int a, int b, long *c);
int main()
{
int a,b;
long c;
int state = 1;
printf("Enter a: ");
printf("(q to quit)");
while( scanf("%d",&a)==state)
{
printf("Enter b: ");
printf("(q to quit)");
while( scanf("%d",&b)==state)
{
fun(a, b, &c);
printf("The result is: %ld\n", c);
}
}
return 0;
}
void fun(int a, int b, long *c)
{
/**********Program**********/
*c = 100*(a%100)+b%100;
/********** End **********/
}
I tried removing the * and found that the result was 16. It is wrong but not know why
The parameter long *c means c is an address of a variable (in this case the variable in main() is called c and you need to call the function like this fun(a, b, &c)).
When you want to update the value stored at that address c the syntax is *c = .... If you do c = ... you are updating the address of the variable which has no external effect.
Alternatively, you could change your function to return a value and the call would then look like this:
c = fun(a, b);
and the function would be:
int fun(int a, int b) {
return 100*(a%100)+b%100;
}
Im trying to understand pointers as function parameters, and in one of the programs there is a segmentation error I can't fix. Firstly, why to use pointers in function arguments? and Why is this error showing?
#include <stdio.h>
void square_it(int* a)
{
printf("The final value is: %d\n", *a * *a);
}
int main()
{
int* input;
puts("This program squares the input integer number");
puts("Please put the number:");
scanf("%d", &input);
square_it(input);
return 0;
}
int* input; does not allocate memory for an int. It mearly makes it possible to make input point at an int (allocated elsewhere). Currently, by dereferencing it (like you do with *a), you make your program have undefined behavior. If you really want an intermediate pointer variable for this, this example shows how it could be done:
#include <stdio.h>
void square_it(int *a) {
*a *= *a; // same as *a = *a * *a;
}
int main() {
int data;
int* input = &data; // now `input` points at an `int`
puts("This program squares the input integer number");
puts("Please put the number:");
// check that `scanf` succeeds:
if(scanf("%d", input) == 1) { // don't take its address, it's a pointer already
square_it(input);
// since `input` is pointing at `data`, it's actually the value of `data`
// that is affected by `scanf` and `square_it`, which makes the below work:
printf("The final value is: %d\n", data);
}
}
Without an intermediate pointer variable:
#include <stdio.h>
void square_it(int *a) {
*a *= *a;
}
int main() {
int input; // note that it's not a pointer here
puts("This program squares the input integer number");
puts("Please put the number:");
if(scanf("%d", &input) == 1) { // here, taking the address of `input` makes sense
square_it(&input); // and here too
printf("The final value is: %d\n", input);
}
}
Without any pointers at all, it could look like this:
#include <stdio.h>
int square_it(int a) {
return a * a;
}
int main() {
int input;
puts("This program squares the input integer number");
puts("Please put the number:");
if(scanf("%d", &input) == 1) { // here, taking the address of `input` makes sense
int result = square_it(input);
printf("The final value is: %d\n", result);
}
}
This is the working code:
#include <stdio.h>
void square_it(int* a)
{
printf("The final value is: %d\n", *a * *a);
}
int main()
{
int i = 0;
int* input = &i;
puts("This program squares the input integer number");
puts("Please put the number:");
scanf("%d", input);
square_it(input);
return 0;
}
There are some errors in the original code:
According to the man-pages to scanf, it takes a format string and then the address of where to store the input.
You gave it the address of a pointer (eg. an int**), which is not what scanf expects.
Also you need to provide memory to store the input in. The scanf string tells that you want an integer as input. In the above code snippet that is i.
input points to i, so i can give the int*, that is input to scanf. scanf will then write into i. We can then go ahead and put the address of i into the sqare_it function.
Since we did not use the heap, we don't need to worry about memory management.
I am a currently learning C and am stuck on a problem I was assigned.
I have to create a function with a parameter of a int pointer, where the user can input an int, and then print out the int in the main method. Currently, I am able to input data and print it out properly within the method itself. However, when the data passes to the main function, it always prints "32766". How can I approach this problem? Thanks for your help.
int main(void) {
int a;
funct2(&a);
printf("Int is %d", a);
}
void funct2(int *a){
int d;
printf("Enter an Integer:: ");
scanf("%d", &d);
printf("%d\n", d);
a = &d;
}
You wrote a = &d; to try to return your integer via pointer. Unfortunately that sets a in funct2 to point to d instead. What you want is *a = d;.
I'm trying to write a program where the user introduces an array numbers and alphabethic characters. Then the program reads the array, if he sees a number, the program should push that number into one stack. However, if he sees an alphabetical character, the last number pushed is popped.
So far I have this code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 20
int top, i;
void push (double stack[], double x, int top)
{
stack[top] = x;
}
int pop (double stack[])
{
double x;
stack [top]=x;
return x;
}
void display (double stack[],int top)
{
int i;
printf ("\n The stack is: ");
for (i=0; i<=top; i++)
{
printf ("%lf\n",stack[i]);
}
}
void main()
{
int r;
int stack[10];
char array[10];
printf("introduce the numbers");
fgets(array,MAX,stdin);
int l;
r=strlen(array);
top=0;
for (l=0;l<=r;l++)
{
int n;
if (isdigit(array[l]))
{
push(stack,array[l],top);
top=top+1;
}
if (islower(array[l]))
{
pop(stack);
printf("you have popped %d", n);
top=top-1;
}
}
display(stack,top);
}
For some reason the program does not work, if I introduce 22a the output is:
you have popped 4194432
The stack is: 50.00000
50.00000
I am particularly interested in how should I write the pop,push and display to make this program work. How can I do it?
First off, your variable n, the value of which you're printing, is uninitialized and contains whatever garbage that was in the memory at the moment of its creation.
Also, why are you printing it? I think you meant to say n = pop(stack);, right? Otherwise this printing is useless.
Throughout your code you're writing loops the wrong way: for (t=0; t<=threshold; t++). This code will make the loop run threshold + 1 times, but you obviously want only threshold, so do for (t=0; t<threshold; t++) instead.
You're also reading (fgets(array,MAX,stdin);) maximum twenty characters into your array, which can hold only ten characters.
To use strlen on an array, you need it to end with zero (null-terminator). In your code array is not necessarily initialized with zeros, so use memset(array, 0, 10); before using array:
Docs on memset
Tutorial on for loops
void main() is wrong
What to read to learn C
void insert(int* h, int* n)
{
printf("give numbers");
scanf("%d %d", h, n);
}
This is the void function I made. This function is suppose to give me the height(h) and the number of hits(n) the ball hits the ground. These numbers are imported by a user.
If the function above is correct, how do I call it?
You can call it as follows:
int h;
int n;
insert(&h, &n);
Where the & means "take the address of".
But be careful: Your function has NO error-handling for erroneous user-input.
You can call it in two ways:
// Method 1
int h, n;
insert(&h, &n);
// Method 2 (if you need to return the pointers or anything else weird for some reason
// I think this is useful in some cases when you are using a library that requires you
// to pass in heap-allocated memory
int *h = malloc(sizeof(int));
int *n = malloc(sizeof(int));
if(h == NULL || n == NULL)
exit(1);
insert(h, n);
// Stuff
free(h);
free(n);
h = n = NULL;
insert(&h, &n)
The & operator gets the address of the variables (a pointer to it), and then passes those pointers to the function. Scanf then uses those points as places to write the values the user enters.
It's mostly correct, but that printf() isn't guaranteed to be shown. stdout may be line buffered, so you would need to issue a fflush().
Here is what your what your code should look like:
#include <stdio.h>
void insert(int*, int*)
int main()
{
int n, h;
insert(&h, &n);
return 0;
}
void insert(int* h, int* n)
{
printf("give numbers");
scanf("%d %d", h, n);
}
However like #Oli your program would break if I input a, 3.5, or anything not an int.