char **函数参数 [英] char** function parameters

查看:123
本文介绍了char **函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下功能:


void doSomething(char ** array,int size);


通常情况下,我会用一个数组来调用函数,例如3

元素...


char * strs [] = {" str1"," str2" ;," STR3" };

doSomething(strs,3);


工作正常。但是,如果我只想传递一个char *,我会认为这将是有效的....


char * str =" str1" ;

doSomething(& str,1);


它汇编了一个关于不兼容指针类型的警告,并且

不必要说它不像我预期的那样表现。 " STR1"没有得到

传入函数。我究竟做错了什么?如何将

单个字符*传递给函数?

Suppose I have the following function:

void doSomething(char** array, int size);

Normally, I would call the function with an array of, say, 3
elements...

char* strs[] = { "str1","str2","str3" };
doSomething(strs, 3);

That works fine. But if I want to pass it only one char*, I would
think this would work....

char* str = "str1";
doSomething(&str, 1);

It compiles with a warning about incompatible pointer types, and
needless to say it doesn''t behave as I expected. "str1" does not get
passed into the function. What am I doing wrong? How can I pass a
single char* into the function?

推荐答案

7月19日星期二2005 co***********@hotmail.com 写道:
On Tue, 19 Jul 2005 co***********@hotmail.com wrote:
假设我有以下功能:

void doSomething(char ** array,int size);

通常情况下,我会打电话具有例如3个元素的数组的函数......

char * strs [] = {" str1"," str2"," str3" };
doSomething(strs,3);

这很好用。但是,如果我只想传递一个char *,我会认为这会起作用....

char * str =" str1";
doSomething(& ; str,1);

它汇编了一个关于不兼容指针类型的警告,并且不用说它不像我预期的那样表现。 " STR1"没有得到
传递给函数。我究竟做错了什么?如何将
单个字符*传递给函数?
Suppose I have the following function:

void doSomething(char** array, int size);

Normally, I would call the function with an array of, say, 3
elements...

char* strs[] = { "str1","str2","str3" };
doSomething(strs, 3);

That works fine. But if I want to pass it only one char*, I would
think this would work....

char* str = "str1";
doSomething(&str, 1);

It compiles with a warning about incompatible pointer types, and
needless to say it doesn''t behave as I expected. "str1" does not get
passed into the function. What am I doing wrong? How can I pass a
single char* into the function?




这是一个非常简单的程序。没有编译问题:


/ *用gcc -Wall -pedantic -O3编译* /

#include< stdio.h>

#include< stdlib.h>


void f(char ** t)

{

printf("%s \ n",* t);

}


int main(无效)

{

char * str =" str0" ;;

char * tab [] = {" str1"," str2"," str3" };

f(& str);

f(tab);

返回EXIT_SUCCESS;

}


一切都很顺利。


所以我的猜测是你有另一种问题,这不是直接的

与传递你的指针有关。


-

" Je deteste les ordinateurs:ils font toujours ce que je dis,jamais ce

que je veux!"

"明显的数学突破将是开发一种简单的方法来计算大素数。 (比尔盖茨,未来之路)



Here is a really simple program. There isn''t a compilation problem :

/* Compiled with gcc -Wall -pedantic -O3 */
#include <stdio.h>
#include <stdlib.h>

void f(char **t)
{
printf("%s\n", *t);
}

int main(void)
{
char * str = "str0";
char * tab[] = { "str1", "str2", "str3" };
f(&str);
f(tab);
return EXIT_SUCCESS;
}

Everything goes fine.

So my guess is you''re having another kind of problem, which isn''t directly
related to passing your pointer.

--
"Je deteste les ordinateurs : ils font toujours ce que je dis, jamais ce
que je veux !"
"The obvious mathematical breakthrough would be development of an easy
way to factor large prime numbers." (Bill Gates, The Road Ahead)


co * **********@hotmail.com 写道:
假设我有以下功能:

void doSomething(char ** array ,int size);

通常情况下,我会用一个数组来调用函数,比方说,3
元素......

char * strs [] = {str1"," str2"," str3" };
doSomething(strs,3);

这很好用。但是,如果我只想传递一个char *,我会认为这会起作用....

char * str =" str1";
doSomething(& ; str,1);

它汇编了一个关于不兼容指针类型的警告,并且不用说它不像我预期的那样表现。 " STR1"没有得到
传递给函数。我究竟做错了什么?如何将
单个字符*传递给函数?
Suppose I have the following function:

void doSomething(char** array, int size);

Normally, I would call the function with an array of, say, 3
elements...

char* strs[] = { "str1","str2","str3" };
doSomething(strs, 3);

That works fine. But if I want to pass it only one char*, I would
think this would work....

char* str = "str1";
doSomething(&str, 1);

It compiles with a warning about incompatible pointer types, and
needless to say it doesn''t behave as I expected. "str1" does not get
passed into the function. What am I doing wrong? How can I pass a
single char* into the function?




我发布的内容没有看错,你能发一个小的吗?

但完整的程序在编译时产生警告,以及

究竟是不是按照你的预期行事?


Robert Gamble



I don''t see anything wrong with what you posted, can you post a small
but complete program that produces the warning when compiled, and what
exactly is not behaving as you expected?

Robert Gamble


问题在于我将char [256]视为char *。这是对Stephane程序的修改,以证明我想要做的事情:


#include< stdio.h>

#include< stdlib.h>


void f(char ** t)

{

printf (%s \ n,* t);

}


int main(无效)

{< br $>
char foo [256];

strcpy(foo," bar");


f(& foo); / *错误* /


返回EXIT_SUCCESS;

}


gcc编译器警告是来自不兼容的指针

type,"当我运行程序时打印出来的是垃圾。


因此可以将foo [256]的内容传递给函数

f() ?

The problem was that I was treating a char[256] as a char*. Here is a
modification of Stephane''s program to demonstrate what I want to do:

#include <stdio.h>
#include <stdlib.h>

void f(char **t)
{
printf("%s\n", *t);
}

int main(void)
{
char foo[256];
strcpy(foo, "bar");

f(&foo); /* WRONG */

return EXIT_SUCCESS;
}

The gcc compiler warning is "assignment from incompatible pointer
type," and what prints out when I run the program is garbage.

So is it possible to pass the contents of foo[256] into the function
f()?


这篇关于char **函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆