C 访问函数main中存在的数组
我有以下代码:C 访问函数main中存在的数组,c,C,我有以下代码: #include <stdio.h> void sample(int b[3]) { //access the elements present in a[counter]. for(int i=0;i<3;i++) printf("elements of a array are%d\n",b[i]); } int main() { int count =3; int a[count];
#include <stdio.h>
void sample(int b[3])
{
//access the elements present in a[counter].
for(int i=0;i<3;i++)
printf("elements of a array are%d\n",b[i]);
}
int main()
{
int count =3;
int a[count];
int i;
for(i=0;i<count;i++)
{
a[i]=4;
}
for(i=0;i<count;i++)
{
printf("array has %d\n",a[i]);
}
sample(//pass the array a[count]);
}
#包括
无效样本(int b[3])
{
//访问[计数器]中的元素。
对于(int i=0;i
函数声明
void sample(int b[]);
函数定义
void sample(int b[]) // void sample(int *b)
{
//access the elements present in a[counter].
//You can access array elements Here with the help of b[0],b[1],b[2]
//any changes made to array b will reflect in array a
//if you want to take SIZE into consideration either define as macro or else declare and define function with another parameter int size_array and From main pass size also
}
函数声明
void sample(int b[]);
函数定义
void sample(int b[]) // void sample(int *b)
{
//access the elements present in a[counter].
//You can access array elements Here with the help of b[0],b[1],b[2]
//any changes made to array b will reflect in array a
//if you want to take SIZE into consideration either define as macro or else declare and define function with another parameter int size_array and From main pass size also
}
函数声明
void sample(int b[]);
函数定义
void sample(int b[]) // void sample(int *b)
{
//access the elements present in a[counter].
//You can access array elements Here with the help of b[0],b[1],b[2]
//any changes made to array b will reflect in array a
//if you want to take SIZE into consideration either define as macro or else declare and define function with another parameter int size_array and From main pass size also
}
函数声明
void sample(int b[]);
函数定义
void sample(int b[]) // void sample(int *b)
{
//access the elements present in a[counter].
//You can access array elements Here with the help of b[0],b[1],b[2]
//any changes made to array b will reflect in array a
//if you want to take SIZE into consideration either define as macro or else declare and define function with another parameter int size_array and From main pass size also
}
将参数作为sample(a);
但是,此代码不起作用。不能使用变量作为数组大小传递
#include<stdio.h>
#define SIZE 3
void sample(int b[]) {
//access the elements present in a[counter] .
for(int i=0;i<3;i++){
printf("elements of a array are%d\n",b[i]);
}
}
int main() {
int a[SIZE];
int i;
for(i=0;i<SIZE;i++){
a[i]=4;
}
for(i=0;i<SIZE;i++){
printf("array has %d\n",a[i]);
}
sample(a);
}
#包括
#定义尺寸3
无效样本(int b[]){
//访问[计数器]中的元素。
对于(int i=0;i将参数作为sample(a);
但是,此代码不起作用。不能使用变量作为数组大小传递
#include<stdio.h>
#define SIZE 3
void sample(int b[]) {
//access the elements present in a[counter] .
for(int i=0;i<3;i++){
printf("elements of a array are%d\n",b[i]);
}
}
int main() {
int a[SIZE];
int i;
for(i=0;i<SIZE;i++){
a[i]=4;
}
for(i=0;i<SIZE;i++){
printf("array has %d\n",a[i]);
}
sample(a);
}
#包括
#定义尺寸3
无效样本(int b[]){
//访问[计数器]中的元素。
对于(int i=0;i将参数作为sample(a);
但是,此代码不起作用。不能使用变量作为数组大小传递
#include<stdio.h>
#define SIZE 3
void sample(int b[]) {
//access the elements present in a[counter] .
for(int i=0;i<3;i++){
printf("elements of a array are%d\n",b[i]);
}
}
int main() {
int a[SIZE];
int i;
for(i=0;i<SIZE;i++){
a[i]=4;
}
for(i=0;i<SIZE;i++){
printf("array has %d\n",a[i]);
}
sample(a);
}
#包括
#定义尺寸3
无效样本(int b[]){
//访问[计数器]中的元素。
对于(int i=0;i将参数作为sample(a);
但是,此代码不起作用。不能使用变量作为数组大小传递
#include<stdio.h>
#define SIZE 3
void sample(int b[]) {
//access the elements present in a[counter] .
for(int i=0;i<3;i++){
printf("elements of a array are%d\n",b[i]);
}
}
int main() {
int a[SIZE];
int i;
for(i=0;i<SIZE;i++){
a[i]=4;
}
for(i=0;i<SIZE;i++){
printf("array has %d\n",a[i]);
}
sample(a);
}
#包括
#定义尺寸3
无效样本(int b[]){
//访问[计数器]中的元素。
对于(int i=0;i,期望它的函数通常必须知道数组的位置和大小。为此,您需要传递一个指向数组第一个元素的指针
sample(a, count);
您的示例函数可能如下所示
void sample(int *b, size_t count) {
for(int i = 0; i < count; i++) {
printf("elements of a array are%d\n",b[i]);
}
}
如果可以确保数组长度至少为3个元素,也可以通过省略count参数来简化此操作。期望它的函数通常必须知道数组的位置和大小。为此,您需要传递指向数组第一个元素的指针
sample(a, count);
您的示例函数可能如下所示
void sample(int *b, size_t count) {
for(int i = 0; i < count; i++) {
printf("elements of a array are%d\n",b[i]);
}
}
如果可以确保数组长度至少为3个元素,也可以通过省略count参数来简化此操作。期望它的函数通常必须知道数组的位置和大小。为此,您需要传递指向数组第一个元素的指针
sample(a, count);
您的示例函数可能如下所示
void sample(int *b, size_t count) {
for(int i = 0; i < count; i++) {
printf("elements of a array are%d\n",b[i]);
}
}
如果可以确保数组长度至少为3个元素,也可以通过省略count参数来简化此操作。期望它的函数通常必须知道数组的位置和大小。为此,您需要传递指向数组第一个元素的指针
sample(a, count);
您的示例函数可能如下所示
void sample(int *b, size_t count) {
for(int i = 0; i < count; i++) {
printf("elements of a array are%d\n",b[i]);
}
}
如果可以确保数组长度至少为3个元素,您还可以通过省略count参数来简化此操作。数组始终作为引用传递。您需要将数组地址传递给实际参数,并使用形式参数中的指针接受它。下面的代码应该适合您
void sample(int *b) //pointer will store address of array.
{
int i;
for(i=0;i<3;i++)
printf("elements of a array are%d\n",b[i]);
}
int main()
{
int count =3;
int a[count];
int i;
for(i=0;i<count;i++)
{
a[i]=4;
}
for(i=0;i<count;i++)
{
printf("array has %d\n",a[i]);
}
sample(a); //Name of array is address to 1st element of the array.
}
void sample(int*b)//指针将存储数组的地址。
{
int i;
for(i=0;i数组始终作为引用传递。您需要将数组的地址传递给实际参数,并使用形式参数中的指针接受它。下面的代码应该适合您
void sample(int *b) //pointer will store address of array.
{
int i;
for(i=0;i<3;i++)
printf("elements of a array are%d\n",b[i]);
}
int main()
{
int count =3;
int a[count];
int i;
for(i=0;i<count;i++)
{
a[i]=4;
}
for(i=0;i<count;i++)
{
printf("array has %d\n",a[i]);
}
sample(a); //Name of array is address to 1st element of the array.
}
void sample(int*b)//指针将存储数组的地址。
{
int i;
for(i=0;i数组始终作为引用传递。您需要将数组的地址传递给实际参数,并使用形式参数中的指针接受它。下面的代码应该适合您
void sample(int *b) //pointer will store address of array.
{
int i;
for(i=0;i<3;i++)
printf("elements of a array are%d\n",b[i]);
}
int main()
{
int count =3;
int a[count];
int i;
for(i=0;i<count;i++)
{
a[i]=4;
}
for(i=0;i<count;i++)
{
printf("array has %d\n",a[i]);
}
sample(a); //Name of array is address to 1st element of the array.
}
void sample(int*b)//指针将存储数组的地址。
{
int i;
for(i=0;i数组始终作为引用传递。您需要将数组的地址传递给实际参数,并使用形式参数中的指针接受它。下面的代码应该适合您
void sample(int *b) //pointer will store address of array.
{
int i;
for(i=0;i<3;i++)
printf("elements of a array are%d\n",b[i]);
}
int main()
{
int count =3;
int a[count];
int i;
for(i=0;i<count;i++)
{
a[i]=4;
}
for(i=0;i<count;i++)
{
printf("array has %d\n",a[i]);
}
sample(a); //Name of array is address to 1st element of the array.
}
void sample(int*b)//指针将存储数组的地址。
{
int i;
对于(i=0;i要将完整数组传递给函数,需要传递其基址,即&a[0]及其长度。可以使用以下代码:
#include<stdio.h>
#include<conio.h>
void sample(int *m,int n)
{
int j;
printf("\nElements of array are:");
for(j=0;j<n;j++)
printf("\n%d",*m);
}
int main()
{
int a[3];
int i;
for(i=0;i<3;i++);
{
a[i]=4;
}
printf("\nArray has:");
for(i=0;i<3;i++)
{
printf("\n%d",a[i]);
}
sample(&a[0],3)
getch();
return 0;
}
#包括
#包括
无效样本(int*m,int n)
{
int j;
printf(“\n数组元素为:”);
对于(j=0;j要将完整数组传递给函数,需要传递其基址,即&a[0]及其长度。可以使用以下代码:
#include<stdio.h>
#include<conio.h>
void sample(int *m,int n)
{
int j;
printf("\nElements of array are:");
for(j=0;j<n;j++)
printf("\n%d",*m);
}
int main()
{
int a[3];
int i;
for(i=0;i<3;i++);
{
a[i]=4;
}
printf("\nArray has:");
for(i=0;i<3;i++)
{
printf("\n%d",a[i]);
}
sample(&a[0],3)
getch();
return 0;
}
#包括
#包括
无效样本(int*m,int n)
{
int j;
printf(“\n数组元素为:”);
对于(j=0;j要将完整数组传递给函数,需要传递其基址,即&a[0]及其长度。可以使用以下代码:
#include<stdio.h>
#include<conio.h>
void sample(int *m,int n)
{
int j;
printf("\nElements of array are:");
for(j=0;j<n;j++)
printf("\n%d",*m);
}
int main()
{
int a[3];
int i;
for(i=0;i<3;i++);
{
a[i]=4;
}
printf("\nArray has:");
for(i=0;i<3;i++)
{
printf("\n%d",a[i]);
}
sample(&a[0],3)
getch();
return 0;
}
#包括
#包括
无效样本(int*m,int n)
{
int j;
printf(“\n数组元素为:”);
对于(j=0;j要将完整数组传递给函数,需要传递其基址,即&a[0]及其长度。可以使用以下代码:
#include<stdio.h>
#include<conio.h>
void sample(int *m,int n)
{
int j;
printf("\nElements of array are:");
for(j=0;j<n;j++)
printf("\n%d",*m);
}
int main()
{
int a[3];
int i;
for(i=0;i<3;i++);
{
a[i]=4;
}
printf("\nArray has:");
for(i=0;i<3;i++)
{
printf("\n%d",a[i]);
}
sample(&a[0],3)
getch();
return 0;
}
#包括
#包括
无效样本(int*m,int n)
{
int j;
printf(“\n数组元素为:”);
对于(j=0;jsample(a)
记住,在函数的数组参数decl中忽略了主索引,所以要小心。欢迎使用StackOverflow。在发布此类问题时,您应该尝试包括您尝试过的内容和遇到的问题的详细信息,例如,在这种特定情况下,您应该包括您尝试过的内容和您遇到的错误收到。您使用VLA有什么特别的原因吗?我在样本(a[3])中传递了数组iteself,这给了我一个无效的参数。这只是我在学习过程中尝试的随机事件sample(a)
记住,在函数的数组参数decl中忽略了主索引,所以要小心。欢迎使用StackOverflow。在发布此类问题时,您应该尝试包括您尝试过的内容和遇到的问题的详细信息,例如,在这种特定情况下,您应该包括您尝试过的内容和您遇到的错误收到。您使用VLA有什么特别的原因吗?我在示例(a[3])中传递了数组iteself