In this code i want to user to input numbers in the function then print the array in the main method, but when i try to print in the main method it gives me random numbers.
for eg:
1 2 3
gives
1 3 1895586112
void arrayInput(int *arr) {
int x;
int y;
int z;
printf("enter 3 numbers =");
scanf("%d %d %d", &x,&y,&z);
arr[0]=x;
arr[1]=y;
arr[2]=z;
}
int main(int argc, char **argv){
int *arr[3];
arrayInput(&arr);
int i;
for(i=0; i<3; i++)
{
printf("%d ", arr[i]);
}
}
I want to change the array values without changing the method or param type
int *arr[3] is an array of pointers to int. But you want an array of int.
Simply change:
int *arr[3];
arrayInput(&arr);
to
int arr[3];
arrayInput(arr);
This should be covered in your C text book.
Bonus:
You can remplace this convoluted code:
void arrayInput(int *arr) {
int x;
int y;
int z;
printf("enter 3 numbers =");
scanf("%d %d %d", &x,&y,&z);
arr[0]=x;
arr[1]=y;
arr[2]=z;
}
with this:
void arrayInput(int *arr) {
printf("enter 3 numbers =");
scanf("%d %d %d", &arr[0],&arr[1], &arr[2]);
}
There are multiple issues with your code.
First you have declared int *arr[3]; which would mean pointer arrays. For your task a simple array is needed. The array variable contains the address of the first element, so you don't need &.
A simple corrected code is as follows:
#include <stdio.h>
void arrayInput(int arr[]) {
int x;
int y;
int z;
printf("enter 3 numbers =");
scanf("%d %d %d", &x,&y,&z);
arr[0]=x;
arr[1]=y;
arr[2]=z;
}
int main(int argc, char **argv){
int arr[3];
arrayInput(arr);
int i;
for(i=0; i<3; i++)
{
printf("%d ", arr[i]);
}
}
Related
*I am taking the size of array from user, the program runs perfectly when i declare int arr[n]; below the scanf in main but, it when i run following code is behaves differently each time, sometimes take more values, some times shows bus error. Even the value of n changes sometimes. *
//Code
#include <stdio.h>
void read_array(int arr[], int *n);
void print_array(int arr[], int *n);
int main() {
int n;
int arr[] = {};
printf("Enter array length ");
scanf("%d", &n);
printf("\nmain elements %d", n);
read_array(arr, &n);
printf("\nElements main %d", n);
print_array(arr, &n);
return 0;
}
void read_array(int arr[], int *n) {
int i;
printf("\n Elements R_A %d", *n);
printf("\nEnter elements");
for (i = 0; i < *n; i++) scanf("%d", &arr[i]);
}
void print_array(int arr[], int *n) {
int i;
printf("\n");
for (i = 0; i < *n; i++) printf("%d ", arr[i]);
printf("\n Elements P_A %d", *n);
}
When an array is declared without an explicit size, it size is taken to be the number of elements it is initialized with. So This:
int arr[]={};
Declares an array of size 0 which is invalid, and attempting to use it triggers undefined behavior.
int arr[] = {}; is a zero-length array (not supported by the standard) and you access it out of bounds.
If your compiler supports VLAs (variable length arrays), you could use one of those instead.
Example:
#include <stdio.h>
void read_array(int arr[], int *n);
void print_array(int arr[], int n); // no need to send in a pointer to n
int main() {
int n;
printf("Enter array length ");
if (scanf("%d", &n) != 1 || n < 1) return 1; // check that scanf succeeded
printf("\nmain elements %d", n);
int arr[n]; // a VLA of n elements
read_array(arr, &n);
printf("\nElements main %d", n);
print_array(arr, n);
return 0;
}
void read_array(int arr[], int *n) {
printf("\n Elements R_A %d", *n);
printf("\nEnter elements");
int i;
for (i = 0; i < *n; i++) {
if (scanf("%d", &arr[i]) != 1) break; // check that scanf succeeded
}
*n = i; // store the number of elements successfully read
}
void print_array(int arr[], int n) {
int i;
printf("\n");
for (i = 0; i < n; i++) printf("%d ", arr[i]);
printf("\n Elements P_A %d", n);
}
I have to do a short assignment for my introductory C class, where I have to ask for a number "N" from the user and calculate it's factorial. The requirements were for me to create a function with the prototype long int factorial(int N). I managed to do it, but I'm confused as to why my code is working with a specific change I made. Here is my code:
#include <stdio.h>
long int factorial(int);
int main(void)
{
int N;
printf("Enter a number: ");
scanf("%d", &N);
printf("The factorial of %d is: %ld", N, factorial(N));
return 0;
}
long int factorial(int N)
{
long int result=1 ;
int i;
for(i=1; i<=N; i++)
result = result * i;
return result;
}
My code at this point didn't work, and would just return the result of N+1 (if I input 5 for example, it would output 6). I was tweaking random things at this point to see what was the problem, and the removal of "void" in my main function fixed it. The problem is, I don't understand why.
#include <stdio.h>
long int factorial(int);
int main()
{
int N;
printf("Enter a number: ");
scanf("%d", &N);
printf("The factorial of %d is: %ld", N, factorial(N));
return 0;
}
long int factorial(int N)
{
long int result=1 ;
int i;
for(i=1; i<=N; i++)
result = result * i;
return result;
}
Could anyone explain why the removal of void in my code fixed this?
I need to find the smallest and biggest number in the array using pointers; in addition to that, I also need to output the address of these numbers in the main function.
The part I am struggling with is finding the address of the number. The problem is that there are different addresses for the same number. Why is it so?
This is what I get as output:
Please enter a value 0 - 4
Please enter a value 1 - 7
Please enter a value 2 - 2
0x7ffeefbff4fc,4
0x7ffeefbff500,7
0x7ffeefbff504,2
The min value is: 2
The max value is: 7
and address 0x7ffeefbff4f0
#define size 3
void Input (int arr[]);
void AdressOutput (int arr[]);
void MinAndMax (int arr[],int *min,int *max);
int main(void)
{
int arr[size];
int min=0,max=0;
Input(arr);
AdressOutput(arr);
MinAndMax(arr,&min,&max);
printf ("The min value is: %d\n",min);
printf ("The max value is: %d\n and address %p\n",max,&max);
return 0;
}
void Input (int arr[])
{
int i;
int *p=arr;
for (i=0;i<size;i++)
{
printf ("Please enter a value %d - ",i);
scanf ("%d",(p+i));
}
printf ("\n");
}
void AdressOutput (int arr[])
{
int i=0;
int *p=arr;
for (i=0;i<size;i++)
{
printf ("%p,%d\n",(p+i),*(p+i));
}
}
void MinAndMax (int arr[],int *min,int *max)
{
int i=0;
int *p;
p=arr;
*min=*p;
*max=*p;
for (i=0;i<size;i++)
{
if(*(p+i)>*max) //finding max
*max=*(p+i);
}
for (i=0;i<size;i++)
{
if(*(p+i)<*min)//finding min
*min=*(p+i);
}
}
I don't understand why the address changes and how can I create a function which will find addresses and will allow me to print them out in the main function?
In the function MinAndMax arr is your input parameter (parameter given to a function for input) and max and min are output parameters (parameter given to a function for storing output). the function would get the addresses of where to store output from output parameters. output parameters are always addresses, ie the value in these parameters point to the memory where you ultimately want to store your output.
What do you want to store? do you want to store an int OR do you want an address which points to int
you want to print the address of min and max values, so you want to store addresses which points to int
in main function you have declared int min=0,max=0;, these can't store addresses, thay can store only int. so lets change that to int *min_address, *max_address;
you have declared function MinAndMax as void MinAndMax (int arr[],int *min,int *max);. for parameters min and max this means addresses which point to int.
but as you want to store addresses, so what you want in your function is: addresses of memory cells which store the address which points to int. in other words you want a double pointer. so lets change the function declaration to void MinAndMax (int arr[], int **min_address, int **max_address);
here is the corrected code:
#include<stdio.h>
#define size 3
void Input (int arr[]);
void AdressOutput (int arr[]);
void MinAndMax (int arr[], int **min_address, int **max_address);
int main(void)
{
int arr[size];
int *min_address, *max_address;
Input(arr);
AdressOutput(arr);
MinAndMax(arr, &min_address, &max_address);
printf("The min value is: %d\n and adress %p\n", *min_address, min_address);
printf("The max value is: %d\n and adress %p\n", *max_address, max_address);
return 0;
}
void Input (int arr[])
{
int i;
int *p=arr;
for (i=0;i<size;i++)
{
printf("Please enter a value %d - ",i);
scanf("%d",(p+i));
}
printf ("\n");
}
void AdressOutput (int arr[])
{
int i=0;
int *p=arr;
for (i=0;i<size;i++)
{
printf("%p,%d\n",(p+i),*(p+i));
}
}
void MinAndMax (int arr[], int **min_address, int **max_address)
{
int i=0;
int *p;
p=arr;
*min_address=p;
*max_address=p;
for (i=0;i<size;i++)
{
if(*(p+i)>**max_address) //finding max
*max_address=(p+i);
}
for (i=0;i<size;i++)
{
if(*(p+i)<**min_address) //finding min
*min_address=(p+i);
}
}
Here's my problem :
I have a program where the user have to fill this :
a 2d array of a list of elements from which i can at most take one element ( we'll call it exclusionArr )
a 2d array of a list of elements from which i have to at least take one element ( we'll call it inclusionArr )
an array with the elements i have to keep ( we'll call it keepArr )
an array with the elements i can't use ( we'll call it throwArr )
Now on the first step i need to check if any row of exclusionArr contain an element of keepArr and if it does then i need to add every element of that row but that one in throwArr.
One row cannot contain more than one element of keepArr, i'll make a function to check for that and return an error if it is. ( you can at most keep one item from the rows of exclusionArr so having 2 elements of keepArr on the same row is a problem )
I need help to add element to an array, i can't seem to be able to store those values in my array (more like i don't really know how to, still new to C).
Here's the function i made so far :
void interdiction(int *throwArr[], int *throw_size, int keepArr[], int keep_size, int x, int y, int exceptionArr[x][y]) {
int i, j, k, count=0;
while(count<keep_size) {
for(i=0;i<x;i++) {
for(j=0;j<y;j++) {
if (exceptionArr[i][j] == keepArr[count]) {
for (k=0;k<y;k++) {
if(k!=j) {
printf("\nElement %d of exclusion %d inserted in throwArr",exceptionArr[i][k], i);
*throw_size+=1;
throwArr[*throw_size]=exceptionArr[i][k];
}
else printf("\nelement to keep found in exclusion %d in position %d", i, j);
}
}
}
}
count++;
}
}
I would like it to change the throwArr that i put in the function so that it adds on the already existing array every element on the current row except the element that's in keepArr.
I don't know if it's relevant or not but for throwArr when initializing it i allocate a lot of extra memory so i could perform changes without running out of space so i don't know if i need to realloc memory in the function for the changes done.
Any help would be greatly appreciated !
EDIT : Here's the full code
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
void interdiction(int *throwArr, int throw_size, int *keepArr, int keep_size, int x, int y, int exclusionArr[x][y]);
static int compare (void const *a, void const *b);
void noDuplicate( int arr[], int *size );
void xclusion_alloc (int x, int y, int(**aptr)[x][y]);
void xclusion_print (int x, int y, int array[x][y]);
void xclusion_fill (int x, int y, int array[x][y]);
void main(){
int throw_size, keep_size, rexc, lexc;
int i, j;
int nbObjets=7, *throwArr, *keepArr, (*exclusionArr)[rexc][lexc];
printf("\nHow many exclusions :");
scanf("%d", &rexc);
printf("\nHow many elements in each exclusion :");
scanf("%d", &lexc);
xclusion_alloc(rexc,lexc,&exclusionArr);
xclusion_fill(rexc,lexc,*exclusionArr);
xclusion_print(rexc,lexc,*exclusionArr);
printf("\nHow many elements do we have to keep :");
scanf("%d", &keep_size);
keepArr=malloc(nbObjets*sizeof(int));
printf("\nWhat are they :");
for(i=0;i<keep_size;i++){
scanf("%d",&keepArr[i]);
}
qsort(keepArr, keep_size, sizeof *keepArr, compare);
noDuplicate(keepArr, &keep_size);
printf("\nHow many elements we can't use :");
scanf("%d", &throw_size);
throwArr=malloc(nbObjets*sizeof(int));
printf("\nWhat are they :");
for(i=0;i<throw_size;i++){
scanf("%d",&throwArr[i]);
}
qsort(throwArr, throw_size, sizeof *throwArr, compare);
noDuplicate(throwArr, &throw_size);
interdiction(throwArr, throw_size, keepArr, keep_size, rexc, lexc, *exclusionArr);
printf("\nOur array of elements we can't use : ");
for (i=0;i<throw_size;i++){
printf("%d ", throwArr[i]);
}
}
static int compare (void const *a, void const *b){
int const *pa = a;
int const *pb = b;
return *pa - *pb;
}
void interdiction(int *throwArr, int throw_size, int *keepArr, int keep_size, int x, int y, int exclusionArr[x][y]){
int i, j, k, count=0;
while(count<keep_size){
for(i=0;i<x;i++) {
for(j=0;j<y;j++) {
if (exclusionArr[i][j] == keepArr[count]) {
for (k=0;k<y;k++){
if(k!=j){
printf("\nElement %d of exclusion %d inserted in the array of elements we can't use",exclusionArr[i][k], i);
throw_size+=1;
throwArr[throw_size]=exclusionArr[i][k];
}
else printf("\nelement to keep found in exclusion n°%d in position %d", i, j);
}
}
}
}
count++;
}
}
void noDuplicate( int arr[], int *size ) {
int i=0, j=0;
for (i = 1; i < *size; i++) {
if (arr[i] != arr[j]) {
j++;
arr[j] = arr[i];
}
}
*size = (j + 1);
}
void xclusion_alloc (int x, int y, int(**aptr)[x][y]) {
*aptr = malloc( sizeof(int[x][y]) );
assert(*aptr != NULL);
}
void xclusion_fill (int x, int y, int array[x][y]) {
int i, j;
for(i=0; i<x; i++) {
for(j=0; j<y; j++) {
scanf("%d", &array[i][j]);
}
}
}
void xclusion_print (int x, int y, int array[x][y]) {
int i,j;
for(i=0; i<x; i++) {
printf("\nExclusion n°%d :", i);
printf(" { ");
for(j=0; j<y; j++) {
printf("%d ", array[i][j]);
}
printf("}");
printf("\n");
}
}
Sadly the output i'm getting is like this :
How many exclusions :3
How many elements in each exclusion :2
5 3
2 7
4 1
Exclusion n░0 : { 5 3 }
Exclusion n░1 : { 2 7 }
Exclusion n░2 : { 4 1 }
How many elements do we have to keep :1
What are they :5
How many elements we can't use :1 2
What are they :
element to keep found in exclusion n░0 in position 0
Element 3 of exclusion 0 inserted in the array of elements we can't use
Our array of elements we can't use : 2
I managed to make it work by making the following changes to my function :
void interdiction(int **throwArr, int *throw_size, int *keepArr, int keep_size, int x, int y, int exclusionArr[x][y]){
int i, j, k, count=0;
while(count<keep_size){
for(i=0;i<x;i++) {
for(j=0;j<y;j++) {
if (exclusionArr[i][j] == keepArr[count]) {
for (k=0;k<y;k++){
if(k!=j){
printf("\nElement %d of exclusion %d inserted in the array of elements we can't use",exclusionArr[i][k], i);
(*throwArr)[*throw_size]=exclusionArr[i][k];
*throw_size+=1;
}
else printf("\nelement to keep found in exclusion n°%d in position %d", i, j);
}
}
}
}
count++;
}
}
Okay I think you need to change those lines as following. Also you made a mistake when you were testing your program.
void interdiction(int *throwArr, int throw_size, int *keepArr, int *keep_size, int x, int y, int exclusionArr[x][y]);
interdiction(throwArr, throw_size, keepArr, &keep_size, rexc, lexc, *exclusionArr);
void interdiction(int *throwArr, int throw_size, int *keepArr, int *keep_size, int x, int y, int exclusionArr[x][y]) {
...
throwArr[*throw_size]=exclusionArr[i][k]; // these two lines switched order
*throw_size+=1;
...
}
This question already has answers here:
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
Closed 8 years ago.
I want to use a function that receive a pointer to an array together with its size and sets all element value to zero.
But the value of the array was checked in the function, the value is not 1234 which should be 1234 if correct, because the input value is 1234.
Just want to know where's the mistake in my following code.
#include <stdio.h>
#include <stdlib.h>
void receive(int *point, const int size);
int main(void) {
int a;
int *b;
scanf("%d", &a);
b=(int*)malloc(sizeof(int)*a);
printf("size of input: %d\n", sizeof(b));
receive(b, sizeof(b));
free(b);
return 0;
}
void receive(int *point, const int size) {
int c=sizeof(*point);
printf("filling the array to zero");
int i;
for (i=0; i<c; i++) {
printf("\n previous_value:%d\n", point[i]);
point[i]=0;
printf(", current_value %d\n", point[i]);
}
}
I changed a few incorrect statements, the resulting code is:
#include <stdio.h>
#include <stdlib.h>
void receive(int *point, const int size);
int main(void) {
int a;
int *b;
scanf("%d", &a);
b= malloc(sizeof(int)*a); //removed implicit declaration of malloc return value
printf("size of input: %d\n", a);
receive(b, a); //changed sizeof(b) to a as second argument of function
free(b);
return 0;
}
void receive(int *point, const int size) {
//removed int c = sizeof(*point); unnecessary statement
printf("filling the array to zero");
int i;
for (i=0; i<size; i++) { //substituted c with size
printf("\n previous_value:%d\n", point[i]);
point[i]=0;
printf(", current_value %d\n", point[i]);
}
}