Anonymous
Kirk
Thank you! i see now
Ольга
Good day. As advised, I solved the problem with gets (), but still can not find an example on the Internet how to compare the elements of the structure and data from the user. I will be glad for any help
https://onlinegdb.com/iYSv_SNbY
Ольга
How can I pass a dynamic array to a function? I still can not achieve the right option. I will be grateful for the hint
Vlad
Vlad
Or something like that
Vlad
Is it C or C++?
Ольга
C, but I have 2d matrix
Ольга
It is same like you say?
Vlad
Vlad
Is it dynamically allocated?
Vlad
If so you'd have to pass two sizes
Vlad
And float**
Vlad
But it won't work for arrays on stack
Vlad
For arrays on stack you'd have to create a pointer array for it to be compatible
Vlad
Vlad
Array with automatic lifetime duration
Ольга
Type it will be in the stack even if the size of the array is set by the user. Yes?
Vlad
Vlad
In C there's also VLA which does what you've described with the same syntax
Vlad
Those are also on stack.
Vlad
But allocated through alloca
Vlad
Anyway. The point is that their type decays into T*
Vlad
Not T**
Vlad
If your matrix has a flat buffer(i.e T*). Then you'd have to address it as arr[i * width + j]
Vlad
So you pass T* and two sizes into the function
Ольга
Oh Okey I try
Boom
hey, please I have doubt about c program
Boom
is there anyone to help me
Ehsan
Boom
#include <stdio.h>
void main()
{
int age[100][10] = {0};
int person;
char name[28][10] = {0}, email[28] = {0};
printf("Enter the number of travellers\n");
scanf("%d", &person);
for (int i = 0; i < person; i++)
{
printf("Enter the name of person %d\n", (i + 1));
scanf("%s", &name[i]);
printf("Enter the age of person %d \n", (i + 1));
scanf("%d", &age[i]);
}
for (int j = 0; j < person; j++)
{
printf(" the name of person %d is %s \n", (j + 1), name[j]);
printf(" the age of person %d is %d \n", (j + 1), age[j]);
}
}
Boom
#include <stdio.h>
void main()
{
int age[100][10] = {0};
int person;
char name[28][10] = {0}, email[28] = {0};
printf("Enter the number of travellers\n");
scanf("%d", &person);
for (int i = 0; i < person; i++)
{
printf("Enter the name of person %d\n", (i + 1));
scanf("%s", &name[i]);
printf("Enter the age of person %d \n", (i + 1));
scanf("%d", &age[i]);
}
for (int j = 0; j < person; j++)
{
printf(" the name of person %d is %s \n", (j + 1), name[j]);
printf(" the age of person %d is %d \n", (j + 1), age[j]);
}
}
in these I'm getting garbage value for age
Boom
#include <stdio.h>
void main()
{
int age[100][10] = {0};
int person;
char name[28][10] = {0}, email[28] = {0};
printf("Enter the number of travellers\n");
scanf("%d", &person);
for (int i = 0; i < person; i++)
{
printf("Enter the name of person %d\n", (i + 1));
scanf("%s", &name[i]);
printf("Enter the age of person %d \n", (i + 1));
scanf("%d", &age[i]);
}
for (int j = 0; j < person; j++)
{
printf(" the name of person %d is %s \n", (j + 1), name[j]);
printf(" the age of person %d is %d \n", (j + 1), age[j]);
}
}
please tell me what i'm doing wrong
Ehsan
Ehsan
it should be: int age[10];
Vlad
Ehsan
Boom
Vlad
And then make an array of structs
Boom
Ehsan
int age[10]; // 10 people ages
Boom
Boom
Boom
im sorry, im so dumb
Ehsan
no worries
Vlad
For the strings
Ehsan
char[28][10]
this is different than other data types, char is a primitive data type that stores a character. If you make a string (which what names are essentially) you need multiple characters; that's why you make it two dimensional.
Ehsan
also it should be
char[10][28]
Vlad
Ehsan
which means (10 names with maximum of 28 character for each name)
Vlad
In C arrays are row margin
Ehsan
Boom
thank you all for being so supportive
Ehsan
Boom
thank you so much
Ehsan
np
Vlad
Vlad
First things first. What does the function suma supposed to do?
Vlad
2) You return from it but never assign the value
Vlad
3) You cast pointer to a function as a int**
Vlad
Btw you can't return a pointer to the local array
Vlad
Array dies right after the function exit
Ольга
Ольга
I tried to fix it, but I'm not sure that anything has gotten better
https://onlinegdb.com/aXNM3xhUx
Vlad
Vlad
Sum of columns?
Vlad
then you'd have to return int*
Vlad
int* sums = malloc(cols * sizeof *sums);