Newbee数组指针问题 [英] Newbee array pointer question

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

问题描述

为什么下面的代码不起作用?

我想在函数中初始化一个字符串,但是我获得了那些STATUS_ACCESS_VIOLATION异常的


#include< stdio.h>

#include< stdlib.h>


void init(char * buffer)

{

buffer =(char *)malloc(6 * sizeof(char));

buffer [0] =''H'';

buffer [1] =''e'';

buffer [2] =''l'';

buffer [3] ='' l'';

buffer [4] =''o'';

buffer [5] =''\''';

}


int main()

{

char * buffer;

init(缓冲区);

printf("%s",buffer);

}

任何解决方法或如何解决此问题?

TIA,

Martin

Why ist the following code not working?
I want to initialize a string in function, but I am
getting those STATUS_ACCESS_VIOLATION exceptions.
#include <stdio.h>
#include <stdlib.h>

void init(char* buffer)
{
buffer = (char*) malloc(6 * sizeof(char));
buffer[0] = ''H'';
buffer[1] = ''e'';
buffer[2] = ''l'';
buffer[3] = ''l'';
buffer[4] = ''o'';
buffer[5] = ''\0'';
}

int main()
{
char* buffer;
init(buffer);
printf("%s", buffer);
}
Any workaround or how to for this?
TIA,
Martin

推荐答案

Martin Andert< ma ******** ***@gmx.de>潦草地写道:
Martin Andert <ma***********@gmx.de> scribbled the following:
为什么以下代码不起作用?
我想在函数中初始化一个字符串,但是我正在获取那些STATUS_ACCESS_VIOLATION异常。


#include< stdio.h>
#include< stdlib.h>
void init(char * buffer)
{
buffer =(char *)malloc(6 * sizeof(char));
Why ist the following code not working?
I want to initialize a string in function, but I am
getting those STATUS_ACCESS_VIOLATION exceptions.
#include <stdio.h>
#include <stdlib.h> void init(char* buffer)
{
buffer = (char*) malloc(6 * sizeof(char));




这是一个非常常见的新手问题。即使参数的类型是指针,C参数传递也是严格按值计算的。因此,你只需要在这里修改init()函数的缓冲区本地副本。尝试

将指针传递给缓冲区。


-

/ - Joona Palaste(pa ***** @ cc.helsinki.fi)-------------芬兰-------- \

\ --------- -----------------------------------------------规则! -------- /

不,玛吉,不是阿兹台克人,奥尔梅克! Ol-mec!

- Lisa Simpson



This is a very common newbie problem. C parameter passing is strictly
by value, even if the parameter''s type is a pointer. Therefore you''re
only modifying the init() function''s local copy of buffer here. Try
passing a pointer to buffer instead.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"No, Maggie, not Aztec, Olmec! Ol-mec!"
- Lisa Simpson


Martin Andert写道:
Martin Andert wrote:

为什么会这样以下代码不起作用?
我想在函数中初始化一个字符串,但是我正在获取那些STATUS_ACCESS_VIOLATION异常。

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

void init(char * buffer)


char * init(void)

{

char * buffer;


所以你不会造成内存泄漏,并且可以返回指针。

{
buffer =(char *)malloc(6 * sizeof(char));


buffer = malloc(6);


足够并且更好。根据定义,sizeof char是1.

buffer [0] =''H'';
buffer [1] =''e'';
buffer [2] = ''l'';
缓冲区[3] =''l'';
缓冲区[4] =''o'';
缓冲区[5] =''\ 0 '';


返回缓冲区; }

int main()
{char * buffer;
init(缓冲区);


buffer = init();

printf("%s",buffer);
}

任何变通方法或如何做到这一点?

Why ist the following code not working?
I want to initialize a string in function, but I am
getting those STATUS_ACCESS_VIOLATION exceptions.

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

void init(char* buffer)
char *init(void)
{
char *buffer;

so you don''t create a memory leak, and can return the pointer.
{
buffer = (char*) malloc(6 * sizeof(char));
buffer = malloc(6);

suffices and is better. By definition sizeof char is 1.
buffer[0] = ''H'';
buffer[1] = ''e'';
buffer[2] = ''l'';
buffer[3] = ''l'';
buffer[4] = ''o'';
buffer[5] = ''\0'';
return buffer; }

int main()
{
char* buffer;
init(buffer);
buffer = init();
printf("%s", buffer);
}

Any workaround or how to for this?




见上文。


-

查克F(cb********@yahoo.com)(cb********@worldnet.att.net)

可用于咨询/临时嵌入式和系统。

< http://cbfalconer.home.att.net>使用worldnet地址!



See above.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


Martin Andert写道:
Martin Andert wrote:

为什么以下代码不起作用?
我想在函数中初始化一个字符串,但是我正在获取那些STATUS_ACCESS_VIOLATION异常。

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

void init(char * buffer)
{/> buffer =(char *)malloc(6 * sizeof(char));
buffer [0] =' 'H'';
buffer [1] =''e'';
buffer [2] =''l'';
buffer [3] =''l'';
buffer [4] =''o'';
缓冲区[5] =''\ 0'';
}

int main()
{
char * buffer;
init(缓冲区);
printf("%s",buffer);
}

任何解决方法或如何做到这一点?

Why ist the following code not working?
I want to initialize a string in function, but I am
getting those STATUS_ACCESS_VIOLATION exceptions.

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

void init(char* buffer)
{
buffer = (char*) malloc(6 * sizeof(char));
buffer[0] = ''H'';
buffer[1] = ''e'';
buffer[2] = ''l'';
buffer[3] = ''l'';
buffer[4] = ''o'';
buffer[5] = ''\0'';
}

int main()
{
char* buffer;
init(buffer);
printf("%s", buffer);
}

Any workaround or how to for this?



#include< stdio.h>

#include< stdlib.h>

void init(char ** buffer)

{

* b uffer = malloc(6 * sizeof ** buffer);

if(* buffer!= NULL){

buffer [0] [0] =''H'' ;

buffer [0] [1] =''e'';

buffer [0] [2] =''l'';

buffer [0] [3] =''l'';

buffer [0] [4] =''o'';

buffer [0 ] [5] =''\ 0'';

}

}


int main(void)

{

char * buffer;


init(& buffer);

if(buffer! = NULL){

printf("%s \ n",buffer);

免费(缓冲区);

}

返回0;

}


-

pete


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

void init(char **buffer)
{
*buffer = malloc(6 * sizeof **buffer);
if (*buffer != NULL) {
buffer[0][0] = ''H'';
buffer[0][1] = ''e'';
buffer[0][2] = ''l'';
buffer[0][3] = ''l'';
buffer[0][4] = ''o'';
buffer[0][5] = ''\0'';
}
}

int main(void)
{
char *buffer;

init(&buffer);
if (buffer != NULL) {
printf("%s\n", buffer);
free(buffer);
}
return 0;
}

--
pete


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

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