#include<stdio.h>
struct Ques
{
int a;
}Q[5];
void sort(int a[])
{
printf("any sort technique...");
}
void main()
{
sort(Q.a);
}
So this is the sample code.
I Want to access the whole struct element as array.
You want this:
#include<stdio.h>
struct Ques
{
int a;
} Q[5];
void sort(struct Ques array[], int size)
{
printf("any sort technique...");
// just some demo
for (int i = 0; i < size; i++)
printf("array[%d].a = %d\n", i, array[i].a);
}
int main()
{
// put some data into Q
sort(Q, 5);
}
sort needs two parameters:
the pointer to the array to sort
the size of the array (unless you only ever want to sort array of some fixed size
Related
I have to write a function that reads from the keyboard a structure type variable and a function that displays a structure type variable. Subsequently, I have to use these functions to read and display a n number of elements of the structure. That's what I managed to write, but it does not look very correct and logical. I'd be very happy to help. Here's my code :
#include <stdio.h>
struct data{
int d, m, y;
}dt;
void readData(struct data element){
printf("\nData format dd-mm-yyyy : ");
scanf("%d %d %d", &element.d,&element.m,&element.y);
}
void read(struct data element,int n){
for(int i = 0; i < n; i++){
readData(element);
}
}
void display(struct data element){
printf("\n %d.%d.%d\n",element.d,element.m,element.y);
}
void displayN(struct data element, int n){
for(int i = 0; i < n; i++){
display(element);
}
}
int main() {
struct data dd1;
read(dd1,3);
displayN(dd1,3);
return 0;
}
It looks like you want to use an array.
#include <stdio.h>
struct data{
int d, m, y;
}dt;
/* use pointer to an element to modify the data */
void readData(struct data *element){
printf("\nData format dd-mm-yyyy : ");
scanf("%d %d %d", &element->d,&element->m,&element->y);
}
/* use an array to read data (just syntax sugar, this argument element is actually a pointer) */
void read(struct data element[],int n){
for(int i = 0; i < n; i++){
readData(&element[i]);
}
}
/* you don't need to use a pointer here because the value is just printed and not changed */
void display(struct data element){
printf("\n %d.%d.%d\n",element.d,element.m,element.y);
}
/* use an array to print multiple data */
void displayN(struct data element[], int n){
for(int i = 0; i < n; i++){
display(element[i]);
}
}
/* define the number of elements and use that to avoid typo */
#define N 3
int main(void) {
struct data dd1[N]; /* declare an array */
read(dd1,N);
displayN(dd1,N);
return 0;
}
I want to eliminate duplicate values from an array. I am passing the values to a function by an array and after processing, collecting the returned array(processed data) by a pointer. The size of the pointer should be only the unique value present in the returned array. (It is working fine if I display there at the called function.) But in the calling function its showing lesser value when I return the array to the pointer in the calling function.
#include <stdio.h>
int* test(int arr[],int size)
{
int i,j;
int flag=1;
static int local_arr[10];
int cnt=1;
local_arr[0]=arr[0];
for (i=1;i<=size; i++)
{
for(j=0;j<cnt;j++)
{
if(arr[i]==local_arr[j])
{
flag=0;
break;
}
else
flag=1;
}
if(flag==1)
{
local_arr[cnt]=arr[i];
cnt++;
}
}
return local_arr;
}
int main(void) {
// your code goes here
int *p,i;
int arr[] = {1,2,5,7,4,2,4,7,9};
int size = sizeof(arr)/sizeof(arr[0]);
p= test(arr,size);
for(i=0;i<(sizeof(p)/sizeof(p[0]));i++)
{
printf("%d\n",*(p+i));
}
return 0;
}
This is my code. main should have only calling function. testcases() call the test cases run through the program.
#include<stdio.h>
#include"conio.h"
int main()
{
testcases();
}
struct test {
int a[10];
} testDB[5] = {
{1,2,3,4,5,6},
{7,8,9,0,1,2,3,4}
};
void testcases()
{
int i;
for(i=0;i<2;i++)
displaytest(testDB[i].a);
}
displaytest(char *a)
{
int i=0;
while(a[i]!='\0')
{
printf("%d\n",a[i]);
i++;
}
}
I want to display both the arrays. But I am getting only first indexes.
Can anyone help ?
Your passed parameter is not appropriate;
displaytest(char *a) --> void displaytest(int *a)
EDIT: Your while loop won't work, as stated in the first comment.
You can not check like this while(a[i]!='\0')
You have to pass the size of the array as 2nd parameter.
void displaytest(int *a, int size)
{
int i = 0;
while (i < size/sizeof(int))
{
printf("%d\n", a[i]);
i++;
}
}
While calling, you can call like this...
displaytest(testDB[i].a, sizeof(testDB[i].a));
I'm trying to print array of pointer using pointer instead of array but I got this error Segmentation fault at runtime:
enter number of element:5
array[0]=1
array[1]=2
array[2]=3
array[3]=4
array[4]=5
Segmentation fault
This is the code:
#include <stdio.h>
#include <stdlib.h>
int *array;
int n;
void input(int *array,int n);
void display(int *array,int n);
int sum(int *array,int n);
int main (void) {
int result;
printf("enter number of element:");scanf("%d",&n);
input(array,n);
display(array,n);
result=sum(array,n);
printf("sum of array=%d",result);
return 0;
}
void input(int *array,int n){
int j;
array=(int *)malloc(n*sizeof(int));
for(j=0;j<n;j++){
printf("array[%d]=",j);scanf("%d",array+j);
}
}
void display(int *array,int n){
int j;
for(j=0;j<n;j++)
printf("%d\t",*(array+j));
printf("\n");
}
int sum(int *array,int n){
int sum=0,j;
for(j=0;j<n;j++)
sum+=*array+j;
return sum;
}
How can I fixed this code? please somebody explain me what's wrong with that code.
Variable array is a local variable in function input.
As such, it is pointless to set it with array = ..., because this assignment takes effect only inside the function. You should typically pass its address (&array) to any function that needs to change it.
In your specific example, you also have a global variable array, so a quick solution to your problem would be to simply call function input without passing variable array as an argument:
void input(int n)
{
...
array = (int*)malloc(n*sizeof(int));
...
}
int main()
{
...
input(n);
...
}
Note that this is a "dirty" workaround, and you should typically strive to avoid the use of global variables.
To add the clean version to barak's answer:
int input(int ** array, const size_t n)
{
int result = 0;
assert(NULL != array);
(*array) = malloc(n * sizeof(**array));
if (NULL == (*array))
{
result = -1;
}
else
{
size_t j;
for(j = 0; j < n; ++j)
{
printf("array[%zu]=", j);
scanf("%d", (*array) + j); /* still missing error checking here . */
}
}
return result;
}
And call it like this:
if (-1 == input(&array, n))
{
perror("input() failed");
exit(EXIT_FAILURE);
}
Try this input():
void input(int **array,int n){
int j;
*array=(int *)malloc(n*sizeof(int));
for(j=0;j<n;j++){
printf("array[%d]=",j);scanf("%d",*array+j);
}
}
Because C use pass-by-value, if you want to change the value of a variable in a function, you need to pass the address of that variable as the argument to that function.
In this case, you want to change the value of array in input() and the type of array is int *, therefore the prototype of input() should be something like void input (int **array, ...).
this should do..make sure you understand what the others have said..
#include <stdio.h>
#include <stdlib.h>
int *array;
int n;
void input(int **array,int n);
void display(int **array,int n);
int sum(int **array,int n);
int main (void) {
int result;
printf("enter number of element:");scanf("%d",&n);
input(&array,n);
display(&array,n);
result = sum(&array,n);
printf("sum of array= %d",result);
return 0;
}
void input(int **array,int n){
int j;
*array= malloc(n*sizeof(int));
for(j=0;j<n;j++){
printf("array[%d]=",j);
scanf("%d",(*array)+j);
}
}
void display(int **array,int n){
int j;
for(j=0;j<n;j++){
printf("%d\t",*((*array)+j)); // you can use array notation aswell
//array[0][j] will work
}
printf("\n");
}
int sum(int **array,int n){
int sum=0,j;
for(j=0;j<n;j++){
sum += *((*array)+j);
}
return sum;
}
What does *array + j do? Does it evaluate *array and add j to it? Or does it add j to array and then dereference it? Would you be willing to bet $100 on it if I told you you are wrong?
Make your life and the life of anybody reading your code easier by using parentheses, or even better, write array [j].
I'm currently studying pointers to functions and have been practicing on sort array functions.
The point is I input a sequence of numbers into the function and the program will re arrange it in ascending order. It worked just fine when I do a call by value function (I think that's how you call it). However when I try to assign a pointer to function and try to use that pointer instead of the function itself, it returns a bunch of errors. I'm sure the problem is due to the fact that I'm passing an array as an argument to the function POINTER. Here is my code:
#include<stdio.h>
#define SIZE 10
void sort(int a[], int size);
void swap(int *elt1, int *elt2);
main()
{
int i; int array[SIZE]= {1,9,3,2,4,100,43,23,32,12};
void (*fptr)(int array, int SIZE);
fptr = &sort;
(*fptr)(array,SIZE);
/*sort(array, SIZE);*/
for(i=0;i<SIZE;i++)
{
printf("%d\n", array[i]);
}
return 0;
}
void sort(int a[], int size)
{
int pass, j;
for(pass = 0; pass<size;pass++)
{
for(j=0;j<size;j++)
{
if(a[j]>a[j+1])
{
swap(&a[j], &a[j+1]);
}
}
}
}
void swap(int *elt1, int *elt2)
{
int hold;
hold = *elt1;
*elt1 = *elt2;
*elt2 = hold;
}
The first argument of the function is a pointer to int (that is, int *), and not int.
void (*fptr)(int array, int SIZE);
should be
void (*fptr)(int *array, int SIZE);