Hii ,
I have written the following code to improve it for the higher datastructures .
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
int display(int *a , int *b , int *c)
{
a[0] = b;
a[1] = c;
printf("\n%d %d",a[0],a[1]); ------- point 1
printf("\n %d %d",*(a[0]),*(a[1])); ------- point 2
return 1;
}
int main()
{
int *a[5];
int b,c;
scanf("%d %d",&b,&c);
printf("%d %d",b,c);
display(a,&b,&c);
getchar();
}
I get the addresses in point 1 , but i dont get the values in point 2....What have i done wrong ... If my program itself is wrong , please jus give me a sample code that can dereference an array of pointers to get the value pointed by the element of array...
This code shouldn't compile. The signature for display should be int display(int **a , int *b , int *c), because a is a pointer to int* (remember that arrays degrade to pointers). Then, you need to write printf("\n%d %d",*a[0],*a[1]) to dereference the pointers in the array.
#include <stdio.h>
int display(int** a, int* b, int* c)
{
// store the value of b and c on array a
a[0] = b;
a[1] = c;
//print the memory addresses stored in hex format
printf("0x%x 0x%x\n", (int)a[0], (int)a[1]);
//print the values
printf("%d %d\n", *a[0], *a[1]);
return 1;
}
int main()
{
int* a[5];
int b,c;
scanf("%d %d",&b,&c);
printf("%d %d\n",b,c);
display(a,&b,&c);
getchar();
return 0;
}
The signature of display() indicates that a is a pointer. In theory, this might work in C, but gcc gave me an error. What you want to tell the compiler is that you want an array of pointers. I accomplished this with int **a in the function signature. The code below shows how I did this. Also, I cleaned it up a bit, since some of your includes aren't needed, the pointers should be printed as unsigned, display would probably be better as a void, having the "\n" at the beginning of the line was irritating in that the last line of output ended up on my prompt line, and the getchar() serves no real purpose here.
#include<stdio.h>
void display(int **a , int *b , int *c)
{
a[0] = b;
a[1] = c;
printf("%u %u\n", a[0], a[1]);
printf("%d %d\n", *a[0], *a[1]);
}
int main(void)
{
int *a[5];
int b,c;
printf("Enter two integers: ");
scanf("%d %d",&b,&c);
printf("%d %d\n",b,c);
display(a,&b,&c);
}
You want store addresses (pointers) in the vector a. Define your function to accept a vector of pointers:
int display(int *a , int *b , int *c)
This has the advantage that the compiler compiles the code.
Or better: use names that help to remember what you mean.
Related
I want to Prompt the user to enter 3 numbers. Then, swap the first number with the second one, the second number with the third and the third with the first by calling a function called "swap".
Functions in C cannot return more than one value so I decided to create a structure with pointers that I will later use in my function. Then, I created three-pointers that will store the address of each one of the numbers so I can dereference to these numbers in my function (as shown below)
Number.pa = *ppb;
Number.pb = *ppc;
Number.pc = *ppa;
Here's my code:
#include <stdio.h>
void swap(); // a = b, b = c, c = a
struct Numbers {
int *pa, *pb, *pc;
} ;
int main(void) {
struct Numbers Number; // Structure to hold the values of the three variables.
int a, b, c;
int *ppa, *ppb, *ppc;
printf("\n Please enter three integer numbers: ");
scanf("%d%d%d", &a, &b, &c);
ppa = &a; ppb = &b; ppc = &c;
swap(a, b, c, Number, *ppa, *ppb, *ppc);
printf("\n %d \t %d \t %d \n", Number.pa, Number.pb, Number.pc);
}
void swap(int a, int b, int c, struct Numbers Number, int *ppa, int *ppb, int *ppc) {
Number.pa = *ppb;
Number.pb = *ppc;
Number.pc = *ppa;
} ;
Most of the arguments to your swap function are either pointless or the work of sheer guessing (or both). The assignment effectively wants you to "rotate" values from a through c. So do that, and only that.
#include <stdio.h>
void swap(int *pa, int *pb, int *pc);
int main()
{
int a, b, c;
printf("\n Please enter three integer numbers: ");
if (scanf("%d %d %d", &a, &b, &c) == 3)
{
swap(&a, &b, &c);
printf("%d %d %d \n", a, b, c);
}
return 0;
}
void swap(int *pa, int *pb, int *pc)
{
int tmp = *pa;
*pa = *pb;
*pb = *pc;
*pc = tmp;
}
Stop reading more into an assignment than is there. If it sounds simple, it probably is. The warning was due to passing the value of dereferenced pointers to int (so int values) to a function expecting int pointers; not int values. As you can see, you don't need to do any of that (and didn't in the first place).
The following code shows me that for one address I have two values. For an example:
Address is: 0xbfcca1ac <br>
Value is: 5
Address is: 0xbfcca1ac <br>
Value is: -1077108308
What am i doing wrong?
#include <stdio.h>
void Input(int *A, int n) {
int i, x=5;
for(i=0; i<n*n; i++) {
*(A+i) = x;
}
printf("\n\n\n\nAddress is: %p\n", A);
printf("Value is: %d\n", *A);
}
main() {
int A[3][3], i, j, n=3;
Input(A, n);
printf("\nAddress is: %p\n", A);
printf("Value is: %d", *A);
return 0;
}
In your main(), A is a 2D array, so *A is an array of 3 integers. Printing *A using the %d format gives undefined behaviour, since *A is not an int.
In your Input(), A is a pointer to an int, so its value is treated as the address of anint. *A is then the value of an int, which is assumed to be at that address.
The value that main() passes to Input() will the address of A[0]. That has type "pointer to array of 3 int", but will happen to have the value equal to (in main()) &A[0][0].
Compilers will (if configured right) give warnings about your code, since it is passing values to functions of different types that those functions will expect. Since there is (at minimum) potential for undefined behaviour due to mismatch of types (function expecting a parameter of one type, but being given another), you should really not ignore such warnings. Even if you are somehow able to reason through what is happening.
You just make an error in the main when printing the value. Since A is a pointer to a pointer to int you should have: printf("Value is: %d",**A); (double dereference).
You compiler should have warned you for incompatible types when passing arguments, a cast will make your compiler silent: Input((int *)A,n);.
Also use the right prototype for main...
Try this :
#include <stdio.h>
typedef int int_of_three[3];
//Another way of saying int_of_three is int[3]
void Input(int_of_three A[3], int n) //int_of_three *A should also be fine
//provided you play by the limits.
{
int i, x=5;
for(i=0; i<n*n; i++)
{
*(*(A+i)) = x;
}
printf("\n\n\n\nAddress is: %p\n", *A);
// *A decays to another pointer.
printf("Value is: %d\n", **A);
printf("\nAddress is: %p\n", *(A+1));
printf("Value is: %d", *(*(A+1)));
}
main()
{
int_of_three A[3];
int n=3;
Input(A, n);
printf("\nAddress is: %p\n", *A);
printf("Value is: %d", **A);
printf("\nAddress is: %p\n", *(A+1));
printf("Value is: %d", *(*(A+1)));
return 0;
}
The function with the pass by value works like it ought to, I believe, as it prints out the value 0 whenever I enter two numbers. However the adderRef (pass by reference function) doesn't work. All it prints out is "the pass by reference of c is" and that's it. There is no value or anything. I just wanted to inquire whether there was something wrong with my syntax or something....
Okay guys sorry about the question being vague and for my errors. It's my first time asking on stackoverflow and I should have been more mindful. I'm aware of my error and why I made it. I got muddled in class when my teacher was altering the code a bit and I copied it down incorrectly/ Thanks everyone for your help. Yes i was indeed quite dumb .
#include <stdio.h>
#include <stdlib.h>
void adderval(int a, int b);
void adderRef(int *, int *);
int main()
{
int a, b, c = 0;
printf("enter two numbers.\n");
scanf("%d %d", &a, &b);
adderval(a, b);
printf("the pass by value of c is %d \n", c);
adderRef(&a, &b);
printf("the pass by reference of c is \n", c );
return 0;
}
void adderRef(int *a, int *b )
{
int c;
c = *a + *b;
}
void adderval(int a, int b )
{
int c;
c = a + b;
}
corrected as below.
#include <stdio.h>
#include <stdlib.h>
void adderval(int a,int b,int c);
void adderRef(int a,int b,int *c );
int main()
{
int a,b,c=0;
printf("enter two numbers.\n");
scanf("%d %d",&a,&b);
adderval(a,b,c);
printf("the pass by value of c is %d \n",c);
adderRef(a,b,&c);
printf("the pass by reference of c is %d \n",c );
return 0;
}
void adderRef (int a, int b, int *c )
{
*c = a + b;
}
void adderval (int a , int b,int c )
{
c=a+b;
}
I'm trying to make a sort function to sort the order of float value incompatible pointer. and when u run the code, after typing the size of pointed array and input the value, then the running just stopped. I do not know where the problem is, any one can help.
I already corrected the warning, but still now result for running the code
#include <stdio.h>
#include <stdlib.h>
void sort(const int size, float *input, float *output);
int main(void) {
int a;
float *b=&b1;
float *c=&c1;
int i, i1;
printf("input the size\n");
scanf("%d", &a);
b=(float*)malloc(sizeof(int)*a);
c=(float*)malloc(sizeof(int)*a);
for(i=0; i<a ; i++){
scanf("%f", &b[i]);
}
for(i1=0; i1<a; i1++){
c[i1]=b[i1];
printf("%f\n", c[i1]);
}
sort(10, b, c);
free(b);
free(c);
return 0;
}
void sort(const int size, float *input, float *output)
{
void swap( float *element1Ptr, float *element2Ptr);
int pass;
int j;
int i0;
for (pass=0; pass<size-1;pass++)
{
for (j=0; j<size-1;j++){
if(input[j]>input[j+1]){
swap(&input[j], &input[j+1]);
}
}
}
for (i0=0; i0<size; i0++){
output[i0]=input[i0];
printf("%f", output[i0]);
}
}
void swap( float *element1Ptr, float *element2Ptr)
{
float hold=*element1Ptr;
*element1Ptr=*element2Ptr;
*element2Ptr=hold;
}
There were a couple of bugs in your code.
No memory was allocated for c.
You modified the input array in sort.
One print loop looped to 10.
Also, I cleaned up the formatting a bit.
I move the forward declaration so that it is outside the sort function. Not a bug, but programmers expect forward declarations to be put outside any function.
I removed unecessary printf statements and print only the sorted array.
#include <stdio.h>
#include <stdlib.h>
void sort(const int size, const float *input, float *output);
void swap( float *element1Ptr, float *element2Ptr);
int main(void) {
int a;
float *b;
float *c;
int i, i1;
printf("input the size\n");
scanf("%d", &a);
b = malloc(sizeof(float)*a);
c = malloc(sizeof(float)*a);
for(i=0; i<a ; i++){
scanf("%f", &b[i]);
}
sort(a, b, c);
for(i1=0; i1<a; i1++){
printf("%f\n", c[i1]);
}
free(b);
free(c);
return 0;
}
void sort(const int size, float const *input, float *output)
{
int pass;
int j;
int i0;
for (i0=0; i0<size; i0++){
output[i0]=input[i0];
}
for (pass=0; pass<size-1;pass++)
{
for (j=0; j<size-1;j++){
if(output[j]>output[j+1]){
swap(&output[j], &output[j+1]);
}
}
}
}
void swap( float *element1Ptr, float *element2Ptr)
{
float hold=*element1Ptr;
*element1Ptr=*element2Ptr;
*element2Ptr=hold;
}
General advice:
Turn up the warning level of the compiler. Compiler warnings are there for a reason.
Fix 1: First you are assigning two float addresses to pointers c and d-
float *b=&b1;
float *c=&c1;
Then you are allocating memory for it. It has no meaning at all. when you allocate memory the newly allocated memory address is returned to the pointer b and c.
if you want to make 0 to all allocated memory you can use calloc to allocate memory. because it will allocate the memory and clear the data in it and give it to user
float *b= (float *)calloc(a,sizeof(float));
float *c= (float *)calloc(a,sizeof(float));
Fix 2: You are having float pointer. but after allocating memory you are typecasting the memory as int *-
float *b=&b1;
but
b=(int*)malloc(sizeof(int)*a); // don't do this
Instead use-
b=malloc(sizeof(float)*a);
Fix 3: With out allocating memory for float *c you are assigning values to it-
for(i1=0; i1<a; i1++){
c[i1]=b[i1]; // note here. you have not allocated memory for c before
printf("%f\n", c[i1]);
}
Allocate the memory for float *c and do it.
c = malloc(sizeof(float)*a);
A simple program to do your work-
#include <stdio.h>
#include <stdlib.h>
void sort(const int size, float *input);
int main(void) {
int a,i;
float *b;
printf("input the size\n");
scanf("%d", &a);
b=(float*)malloc(sizeof(float)*a);
for(i=0; i<a ; i++){
scanf("%f", &b[i]);
}
sort(a, b);
for (i=0; i<a; i++)
printf("%f\n",b[i]);
free(b);
return 0;
}
void sort(const int size, float *input)
{
int pass,j,temp;
for (pass=0; pass<size-1;pass++)
{
for (j=0; j<size-1;j++){
if(input[j]>input[j+1]){
temp = input[j];
input[j]=input[j+1];
input[j+1]=temp;
}
}
}
}
Don't use unnecessary variables, Other then the important ones! If you want a copy of your input, copy it to another array and to the sorting on the output array, not on input array!
You defined variable b as having type float *
float *b=&b1;
but are trying to assign pointer to int to that variable
b=(int*)malloc(sizeof(int)*a);
First of all there is no sense in the initialization of b
float *b=&b1;
and secondly it seems you want to allocate an array of floats. So you have to write
b = ( float * )malloc( sizeof( float ) * a );
Also you did not allocate memory pointed to by c. So this code
for(i1=0; i1<a; i1++){
c[i1]=b[i1];
printf("%f\n", c[i1]);
}
sort(10, b, c);
is invalid and the program has undefined behaviour. It is also unclear why you are using magic number 10 in sort instead of variable a.
And what is the sense in defining variables b1 and c1?
float b1=0;
float c1=0;
It seems they are not used.
void getnums(int *a, int *b);
int main()
{
int a;
int b;
int c;
getnums(&a,&b);
c = a + b;
printf("a + b = %d\n", c);
return 0;
}
void getnums(int *a, int *b)
{
printf("a:? ");
scanf("%d", a);
printf("b:? ");
scanf("%d", b);
}
Why don't I need ampersands before the a and b in the scanfs? (The code currently works.)
Because scanf takes pointers as its arguments (so that it knows what variable to modify), and a and b are already pointers.
Whenever we scan some input it needs a memory location(i.e. address) to store that value, for simple variables we have to use & - ampersand - to provide that address.
Here, in function getnums, a and b are pointers so they will already contain address, so no need to write & to give the address.