在过去一年左右的时间里,我注意到 StackOverflow 上有一些与 C++ 相关的答案mdspan,但我从未在 C++ 代码中真正看到过这些答案。我尝试在 C++ 编译器的标准库目录和C++ 编码指南中查找它们- 但找不到它们。我确实找到了std::span;我猜它们是相关的——但是如何呢?添加“md”代表什么?
请解释一下这个神秘实体的用途,以及我何时需要使用它。
我目前对如何将字符串数组传递给函数感到困惑。我创建了一个一维数组。我已经做过的方法有效,但似乎多余,我认为有更好的方法可以做到这一点,但我不确定如何做。我试图找到一种方法可以一次将所有4个元素传递给函数。
这是我的代码示例。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void sort(char *,char *,char *, char *);//Function prototype
int main()
{
char *string_database[4]={'\0'};
string_database[0]="Florida";
string_database[1]="Oregon";
string_database[2]="California";
string_database[3]="Georgia";
sort(string_database[0],string_database[1],string_database[2],string_database[3]);
return 0;
}
void sort(char *string1, char *string2, char *string3, char *string4)
{
printf("The string is= %s\n",string1);
printf("The string is= %s\n",string2);
printf("The string is= %s\n",string3);
printf("The string is= %s\n\n\n",string4);
}
Run Code Online (Sandbox Code Playgroud)
在此先感谢您,感谢您对我的问题的任何答复。
我在这里阅读了很多内容并尝试了很多但是我找不到将多维数组传递给C中的函数的方法,更改了一些值并以某种方式返回新数组.找到一种方法将该数组进一步传递给另一个函数并执行相同的操作非常重要.
我想找到一种方法将数组传递给一个函数.然后将它从第一个函数传递给第二个函数,在那里做一些事情(可能打印,也许更改值),然后再次使用它到第一个函数,最后使用主要的那个数组.
我的最后一次尝试是:
void func(int multarray[][columns]){
multarray[0][0]=9;
}
int main(){
int rows;
int columns;
int multarray[rows][columns];
func(multarray);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我也试过这个:
void func(int multarray[rows][columns]){
multarray[0][0]=9;
}
int main(){
int rows;
int columns;
int multarray[rows][columns];
func(multarray);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我也试过这个:
int
getid(int row, int x, int y) {
return (row*x+y);
}
void
printMatrix(int*arr, int row, int col) {
for(int x = 0; x < row ; x++) {
printf("\n");
for (int y = 0; y <col ; y++) {
printf("%d ",arr[getid(row, …Run Code Online (Sandbox Code Playgroud) 我是C的完整菜鸟(<1周),我正在努力掌握如何处理它,虽然我熟悉其他语言的编程.作为第一个目标,我想编写一个函数来对矩阵进行高斯减少.我对算法没有任何问题,但事实证明我不知道如何表示矩阵.为简单起见,我假设我们使用float条目.
第一种天真的方式是使用像数组一样的数组
float naifMatrix[3][3] = {
{2, 1, 3},
{0, -1, 4},
{1, 3, 0}
};
Run Code Online (Sandbox Code Playgroud)
问题是你不能在不知道先验维度的情况下将这样的对象作为参数传递(当然我希望能够使用任意大小的矩阵,这在编译时是未知的).在使用向量并将它们表示为数组时,人们不会看到这个问题.如果我做
float vector[3] = {1, 2, 3};
norm(vector);
Run Code Online (Sandbox Code Playgroud)
它会工作,提供我宣布norm样
norm(float * vector);
Run Code Online (Sandbox Code Playgroud)
当vector通过时,它被转换成&vector[0],并没有太大的信息丢失(基本上一个必须跟踪长度).但我不能只是打电话
gaussReduction(naifMatrix);
Run Code Online (Sandbox Code Playgroud)
并宣布gaussReduction与
gaussReduction(float ** naifMatrix);
Run Code Online (Sandbox Code Playgroud)
因为naifMatrix被转换(并且正确地)转换为指向浮点数组的指针,而不是指向指针的指针.由于我不知道这个数组有多大,我没有看到声明的方法gaussReduction.
当然我可以通过将指针传递给void来作弊,但在解除引用之前,我需要将它转换为正确的类型(float[3] *),这也是我不知道的先验.此外,在我看来,滥用void *一个失败的目的之一是使用C而不是其他语言,这是一种严格的类型检查.
到目前为止,我发现的最佳解决方案是使用结构.矩阵基本上由其条目列表和两个维度给出.所以我能做到
struct matrix {
float * begin;
int rows, columns;
};
Run Code Online (Sandbox Code Playgroud)
并用它作为
struct matrix matrix = {&naifMatrix[0], 3, 3};
Run Code Online (Sandbox Code Playgroud)
问题是这仍然很烦人.首先,struct matrix从双数组中获取a是很难的,第二个必须明确地给出维数.我很乐意用一种"构造函数"函数来包装它,比如
struct …Run Code Online (Sandbox Code Playgroud) 我有一个chars table [] [] []的3D数组,我想将它传递给void函数,以便它可以对它进行更改.我怎样才能做到这一点?
void make(char minor[][][]);
.....
char greater[20][30][50];
make(greater);
Run Code Online (Sandbox Code Playgroud)
我想这不会奏效.
编辑:与此相关的另一个问题:我想创建一个复制函数将字符串复制到数组中 - 我应该如何调用函数中的strcpy?
void copy(char (*minor)[][])
{ char m[50] = "asdasdasd";
strcpy(minor[][],m);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试将二维数组传递给 C++ 中的函数。问题是它的量纲不是普遍常数。我将维度作为用户的输入,然后尝试传递数组。这是我正在做的事情:
/*
* boy.cpp
*
* Created on: 05-Oct-2014
* Author: pranjal
*/
#include<iostream>
#include<cstdlib>
using namespace std;
class Queue{
private:
int array[1000];
int front=0,rear=0;
public:
void enqueue(int data){
if(front!=(rear+1)%1000){
array[rear++]=data;
}
}
int dequeue(){
return array[front++];
}
bool isEmpty(){
if(front==rear)
return true;
else
return false;
}
};
class Graph{
public:
void input(int matrix[][],int num_h){ //this is where I am passing the matrix
int distance;
char ans;
for(int i=0;i<num_h;i++){
for(int j=0;j<num_h;j++)
matrix[i][j]=0;
}
for(int i=0;i<num_h;i++){
for(int j=i+1;j<num_h;j++){
cout<<"Is …Run Code Online (Sandbox Code Playgroud) 可能的重复:
在C中将多维数组作为函数参数传递
将多维数组转换为c ++中的指针
嗨,
我尝试传递2维数组以在C中起作用,并且以下代码有效
void printArray(int a[][4], int size) {
int i = 0;
for (; i < size; ++i) {
int j = 0;
for (; j < size; ++j) {
printf("%d,", a[i][j]);
}
printf("\n");
}
}
Run Code Online (Sandbox Code Playgroud)
但如果我更换"int a[][4]",以"int **a"
它不会工作,任何人都可以告诉有什么区别?
谢谢
我知道二维数组作为一维数组存储在内存中.因此,遵循相同的逻辑,我试图通过引用使用单个指针传递数组,就像对一维数组所做的那样.以下是我的代码:
#include<stdio.h>
void display(int *s)
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("%d ",s[i][j]);
}
printf("\n");
}
}
int main()
{
int s[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
printf("address of the array is %p\n",s);
printf("value is %p\n",*s);
int i;
printf("address of the repective array is\n");
for(i=0;i<3;i++)
{
printf("address of the array is %p\n",s[i]);
}
display(s);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译此获取以下消息时:
twodarray.c: In function ‘main’:
twodarray.c:25:2: warning: passing argument 1 of ‘display’ from incompatible pointer type [enabled by default]
display(s);
^
twodarray.c:2:6: note: expected ‘int **’ but …Run Code Online (Sandbox Code Playgroud) 我的程序中有一个问题.当我将3D int数组CodedGreen传递给函数Green_Decode_Tree时.显示错误消息"无效使用带有未指定边界的数组".我的课程有什么错误?谢谢你的帮助.
for(i=0;i<256;i++){
for(j=0;j<256;j++){
Decode_Tree(green[0], CodedGreen,0,i,j);
}
}
void Green_Decode_Tree(node* tree, int code[][][], int num,int row,int col)
{
int i;
i=num;
if((tree->left == NULL) && (tree->right == NULL)){
fprintf(DecodGreen,"%s\n", tree->ch);
}
else
{
if(code[row][col][num]==1){
i++;
Green_Decode_Tree(tree->left,code,i,row,col);
}
else if (code[row][col][num]==0){
i++;
Green_Decode_Tree(tree->right,code,i,row,col);
}
}
}
Run Code Online (Sandbox Code Playgroud)