简体   繁体   English

在函数中传递指针到字符:语法错误

[英]Passing pointer-to-char in function: syntax error

My question is related to my previous post: 我的问题与我以前的帖子有关:

explicit specialization: syntax error? 显式专业化:语法错误?

I am trying to pass arrays of pointer-to-chars as an argument to a function (which I will later incorporate to a specialized function from previous post), but I cannot get the syntax right. 我试图将指针字符数组作为函数的参数传递(我将在以后的文章中将其合并到专用函数中),但是我无法正确使用语法。

The following were declared under main program: 在主程序中声明了以下内容:

char c0[30] = { "see if this works" };
char c1[30] = { "functions" };
char c2[30] = { "explicit specialization" };
char *d[] = { c0, c1, c2 }; 

the next line prints "functions " as I was expecting: 下一行按照我的预期打印“功能”:

cout << "test print d[1] " << d[1] << endl;

The next step is to test whether I am able to return the character-array that I want to return, but my syntax is incorrect. 下一步是测试我是否能够返回要返回的字符数组,但是我的语法不正确。 The following returns 's' (from c0) instead of an entire char-array: 以下代码返回“ s”(从c0开始),而不是整个char数组:

function call: 函数调用:

cout << "string compare is " << compare2(*d, 3);

function declaration: 函数声明:

char compare2(char const arr2[], int n) {
    char temp;
    temp = arr2[0];

    return temp;

appreciate the help! 感谢您的帮助!

Oh never mind. 哦,没关系。 I figured it out :D 我想通了:D

pointer declaration: 指针声明:

char *(d[]) = { c0, c1, c2 };

function call: 函数调用:

cout << "string compare is " << compare2(d, 3);

function definition: 函数定义:

char * compare2(char * const arr2[], int n) {
    char * temp;
    temp = *(arr2+2);

    return temp;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM