在C基准字符串传递 [英] pass strings by reference in C

查看:80
本文介绍了在C基准字符串传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在遇到麻烦找出如何传回的字符串通过函数的参数。我是新来编程,所以我想这这可能是初学者问题。任何帮助,你可以给将是最AP preciated。这code赛格故障,我不知道为什么,但我提供我的code,以显示我有什么这么远。<​​/ P>

我做这个社区维基,可以随意编辑。

P.S。这是不会的功课。

这是原来的版本

 的#include&LT;&stdio.h中GT;#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;空虚
FN(字符*巴兹,字符*富,字符*巴)
{
     字符* PCH;     / *这是我遇到的麻烦的部分* /     PCH = strtok的(巴兹,:);
     富=的malloc(strlen的(PCH));
     的strcpy(富,PCH);     PCH = strtok的(NULL,:);
     巴=的malloc(strlen的(PCH));
     的strcpy(巴,PCH);     返回;
}INT
主要(无效)
{
     字符* mybaz,* myfoo,* mybar;     mybaz =你好:天下;     FN(mybaz,myfoo,mybar);     fprintf中(标准错误,%s%S,myfoo,mybar);
}

更新这里有一个更新的版本与一些建议实施的:

 的#include&LT;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;#定义MAXLINE 1024空虚
FN(字符*巴兹,焦炭**富,焦炭**吧)
{
     焦线[MAXLINE]
     字符* PCH;     的strcpy(线,巴兹);     PCH = strtok的(行,:);
     *富=(的char *)malloc的(strlen的(PCH)+1);
     (* foo的)的strlen(PCH)] ='\\ n';
     的strcpy(*富,PCH);     PCH = strtok的(NULL,:);
     *栏=(字符*)malloc的(strlen的(PCH)+1);
     (*巴)的strlen(PCH)] ='\\ n';
     的strcpy(*酒吧,PCH);     返回;
}INT
主要(无效)
{
     字符* mybaz,* myfoo,* mybar;     mybaz =你好:天下;     FN(mybaz,&安培; myfoo,&安培; mybar);     fprintf中(标准错误,%s%S,myfoo,mybar);     免费(myfoo);
     免费(mybar);
}


解决方案

第一件事,那些mallocs应该是 strlen的(其他)+1 字节。 C字符串有一个0的字符,以指示结束时,称为NUL终止子,并且它不包括在由函数strlen测得的长度。

接下来的事情,strtok的修改您所搜索的字符串。你是一个指针传递给它到你不允许修改(你不能修改文字字符串)的字符串。这可能是该段错误的原因。因此,而不是使用一个指向不可修改字符串,你可以把它复制到你自己的,修改的缓冲,就像这样:

 字符mybaz [] =你好:天下;

这是用来做什么的放在堆栈上的大小为12字符数组,并复制字符串成数组的字节。它的工作原理,因为编译器知道,在编译时,字符串有多长,并能作出相应的空间。这样可以节省使用malloc为特定副本。

您有引用的问题是,你目前传递的的mybaz,myfoo,并mybar给你的函数。除非你传递的指针应用于myfoo和mybar你不能修改调用者的变量。由于myfoo是一个char *,它的指针是char **:

 无效
FN(字符*巴兹,焦炭**富,焦炭**吧)//取指针到指针*富=的malloc(...); //设置的值由富指出,FN(mybaz,&安培; myfoo,&安培; mybar); //指针传递myfoo和mybar

在你的code函数修改富对 myfoo 绝对没有影响。 myfoo 是未初始化的,所以如果没有第一次两件事情引起的,该段错误是最有可能发生的,当你来到使用来打印未初始化的指针。

一旦你得到了它基本的工作,你可能想添加一些错误处理。 strtok的可以返回NULL如果它没有找到它要找的分隔符,你不能叫的strlen 与空值。 的malloc 可以返回NULL,如果没有足够的内存,并且你也不能叫的strcpy 为NULL。

I'm having trouble figuring out how to pass strings back through the parameters of a function. I'm new to programming, so I imagine this this probably a beginner question. Any help you could give would be most appreciated. This code seg faults, and I'm not sure why, but I'm providing my code to show what I have so far.

I have made this a community wiki, so feel free to edit.

P.S. This is not homework.

This is the original version

#include <stdio.h>

#include <stdlib.h>
#include <string.h>

void
fn(char *baz, char *foo, char *bar)
{
     char *pch;

     /* this is the part I'm having trouble with */

     pch = strtok (baz, ":");
     foo = malloc(strlen(pch));
     strcpy(foo, pch);

     pch = strtok (NULL, ":");
     bar = malloc(strlen(pch));
     strcpy(bar, pch);

     return;
}

int
main(void)
{
     char *mybaz, *myfoo, *mybar;

     mybaz = "hello:world";

     fn(mybaz, myfoo, mybar);

     fprintf(stderr, "%s %s", myfoo, mybar);
}

UPDATE Here's an updated version with some of the suggestions implemented:

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

#define MAXLINE         1024

void
fn(char *baz, char **foo, char **bar)
{
     char line[MAXLINE];
     char *pch;

     strcpy(line, baz);

     pch = strtok (line, ":");
     *foo = (char *)malloc(strlen(pch)+1);
     (*foo)[strlen(pch)] = '\n';
     strcpy(*foo, pch);

     pch = strtok (NULL, ":");
     *bar = (char *)malloc(strlen(pch)+1);
     (*bar)[strlen(pch)] = '\n';
     strcpy(*bar, pch);

     return;
}

int
main(void)
{
     char *mybaz, *myfoo, *mybar;

     mybaz = "hello:world";

     fn(mybaz, &myfoo, &mybar);

     fprintf(stderr, "%s %s", myfoo, mybar);

     free(myfoo);
     free(mybar);
}

解决方案

First thing, those mallocs should be for strlen(whatever)+1 bytes. C strings have a 0 character to indicate the end, called the NUL terminator, and it isn't included in the length measured by strlen.

Next thing, strtok modifies the string you're searching. You are passing it a pointer to a string which you're not allowed to modify (you can't modify literal strings). That could be the cause of the segfault. So instead of using a pointer to the non-modifiable string literal, you could copy it to your own, modifiable buffer, like this:

char mybaz[] = "hello:world";

What this does is put a size 12 char array on the stack, and copy the bytes of the string literal into that array. It works because the compiler knows, at compile time, how long the string is, and can make space accordingly. This saves using malloc for that particular copy.

The problem you have with references is that you're currently passing the value of mybaz, myfoo, and mybar into your function. You can't modify the caller's variables unless you pass a pointer to myfoo and mybar. Since myfoo is a char*, a pointer to it is a char**:

void
fn(char *baz, char **foo, char **bar) // take pointers-to-pointers

*foo = malloc(...);  // set the value pointed to by foo

fn(mybaz, &myfoo, &mybar);  // pass pointers to myfoo and mybar

Modifying foo in the function in your code has absolutely no effect on myfoo. myfoo is uninitialised, so if neither of the first two things is causing it, the segfault is most likely occurring when you come to print using that uninitialised pointer.

Once you've got it basically working, you might want to add some error-handling. strtok can return NULL if it doesn't find the separator it's looking for, and you can't call strlen with NULL. malloc can return NULL if there isn't enough memory, and you can't call strcpy with NULL either.

这篇关于在C基准字符串传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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