返回ponters数组? [英] Return array of ponters?

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

问题描述

大家好,


我是这个小组的新手。我正在用c语言工作。我在指针中投入了
?如何在函数中重新调用指针数组?


例子

main()

{

char * var [2];

char * a [2];

a [0] =" hi";

a [1] =" c language";

var = function(a);

}


char * function (char * a [2])

{

char * b [2];

b = a;

返回(b);

}

在这个程序中,我必须将b值(指针数组)重新调整为var

变量...

谢谢


问候,

priya

Hi all,

I am new to this group.I am working in c language.I have dount
in pointer? how to retun array of pointer in function?


example
main()
{
char *var[2];
char *a[2];
a[0]="hi";
a[1]="c language";
var=function(a);
}

char* function(char *a[2])
{
char *b[2];
b=a;

return(b);
}
In this program i have to retun b value(array of pointer) to var
variable...
Thanks

regards,
priya

推荐答案

main()
{* char * var [2];
char * a [2];
a [0] =" hi";
a [1] =" c language" ;;
var = function(a);


这是错误的。 var不是左值。

}

char * function(char * a [2])
{
char * b [ 2];
b = a;


又错了。 b不是左值。

返回(b);
}

在这个程序中我必须重新调整b值(指针数组) var
变量...
main()
{
char *var[2];
char *a[2];
a[0]="hi";
a[1]="c language";
var=function(a);
This is wrong. var is not an "lvalue".
}

char* function(char *a[2])
{
char *b[2];
b=a;
Again wrong. b is not an "lvalue".

return(b);
}
In this program i have to retun b value(array of pointer) to var
variable...



你不能返回一个指针数组。你可以做的是返回

指针数组的第一个元素的地址。


You cannot return an array of pointers. What you can do is return
the address of the first element of the array of pointers.




嗨。


你不应该使用像这样的指针数组。取而代之的是你需要使用

a指向一个char * whitch会给你你想要的结果。


那么你的例子就会看起来像这样。


例如:

#include< stdio.h>


/ *你应该总是声明函数,以便编译器知道

想要什么。如果没有,你基本上可以给它多少你想要的参数和

他们所有

将是int类型(在这种情况下,女巫在大多数系统上都能正常工作,

但不是

全部)。 * /

char **函数(char ** a);


/ * main总是有int类型,如果没有参数你应该总是

给出无效* /

int main(无效)

{

char * a [2]; / *你的指针数组* /

char ** var; / *指向char * witch的指针可以作为一个

指针的数组* /

a [0] =" hi";

a [1] =" c language";

var = function(a);


printf("%s%s \ n" ,var [0],var [1]);

系统(暂停);

返回0;

}


/ *使用一个参数witch是一个指向char *的指针,并返回一个指向char *的

指针。 * /

char **功能(char ** a)

{

char ** b;


b = a;


/ *你不需要括号,但这并不重要* /

返回b;

}


-

bjrnove


Hi.

You shouldn''t work with an array of pointers like that. Instead you
should use
a pointer to a char* whitch will give you the result you want.

Your example would then look something like this.

example:
#include <stdio.h>

/* You should ALWAYS declare functions so the compiler knows what
argument it
wants. If not you basicly could give it as many arguments you want and
they all
would be the type int (witch in this case will work ok on most systems,
but not
all). */
char** function(char **a);

/* main always has the type int and if no arguments you should always
give void */
int main(void)
{
char *a[2]; /* Your array of pointers */
char **var; /* A pointer to a char* witch could work as an array of
pointers */
a[0] = "hi";
a[1] = "c language";
var = function(a);

printf("%s %s\n", var[0], var[1]);
system("pause");
return 0;
}

/* Takes one argument witch is a pointer to a char* and returns a
pointer to a char*. */
char** function(char **a)
{
char **b;

b=a;

/* You do not need the brackets, but that''s not important */
return b;
}

--
bjrnove




bjrnove写道:

bjrnove wrote:
嗨。

你不应该使用像这样的指针数组。相反,你应该使用
一个指向char * whitch的指针,它会给你你想要的结果。

你的例子看起来就像这样。

示例:

#include< stdio.h>

/ *你应该总是声明函数,以便编译器知道它的论点是什么
想。如果不是你基本上可以给它多少参数你想要
而且它们都是int类型(在这种情况下,巫婆在大多数
系统上都能正常工作,但不是
所有)。 * /
char **函数(char ** a);

/ * main总是有int类型,如果没有参数,你应该总是
给出void * /
int main(void)
{char / a char * a [2]; / *你的指针数组* /
char ** var; / *指向char * witch的指针可以用作数组
的指针* /
a [0] =" hi";
a [1] =" c language" ;
var = function(a);

printf("%s%s \ n",var [0],var [1]);
system( 暂停);
返回0;
}
/ *取一个参数,巫婆是一个指向char *的指针,并返回一个指向char的
指针*。 * /
char **功能(char ** a)
{
char ** b;

b = a;

/ *你不需要括号,但这并不重要* /
返回b;
}

-
bjrnove
Hi.

You shouldn''t work with an array of pointers like that. Instead you
should use
a pointer to a char* whitch will give you the result you want.

Your example would then look something like this.

example:
#include <stdio.h>

/* You should ALWAYS declare functions so the compiler knows what
argument it
wants. If not you basicly could give it as many arguments you want and they all
would be the type int (witch in this case will work ok on most systems, but not
all). */
char** function(char **a);

/* main always has the type int and if no arguments you should always
give void */
int main(void)
{
char *a[2]; /* Your array of pointers */
char **var; /* A pointer to a char* witch could work as an array of pointers */
a[0] = "hi";
a[1] = "c language";
var = function(a);

printf("%s %s\n", var[0], var[1]);
system("pause");
return 0;
}

/* Takes one argument witch is a pointer to a char* and returns a
pointer to a char*. */
char** function(char **a)
{
char **b;

b=a;

/* You do not need the brackets, but that''s not important */
return b;
}

--
bjrnove




我只是想知道编译器如何评估var [0]和var [1]。

var最初被声明为char的双指针。

编译器如何评估var [1]?



I was just wondering how compiler evaluates var[0] and var[1].
var was originally declared as a double pointer to char.
How does compiler evaluates var[1] ?


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

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