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

问题描述




我需要帮助一些让我烦恼的东西。

我能让程序打印出来的唯一方法

如果我在while循环中执行此操作。


i = 0;

while((c = getc(fp))!= EOF){

bp [i ++] = c

}

bp [i] =''\ 0''


而不是


而((c = getc(fp))!= EOF){

* bp ++ = c;

}

* bp =''\ 0'';


为什么我不能让它上班?


这是完整的程序。

我正在运行gentoo / linux。


#include < stdio.h>

#include< stdlib.h>


size_t bufsize = 512;


int main(int argc,char * argv [])

{

FILE * fp;

char * bp;

int i;

int c;


* argv ++;

fp = fopen(* argv," r");


if((bp = malloc(bufsize * sizeof(char *)))== NULL){

printf(不能分配内存。\ n);

返回1;

}


/ * i = 0; * /

while((c = getc(fp))!= EOF){

* bp ++ = c;

/ * bp [i ++] = c * /

}

* bp =' '\0'';

/ * bp [i] =''\ 0''* /

fclose(fp);


printf("%s",bp);


免费(bp);


返回0;

}

Hi

I need help with something thats bugging me for a while.
The only way I can get the program to print something
is if I do this in the while loop.

i = 0;
while ((c = getc(fp)) != EOF) {
bp[i++] = c
}
bp[i] = ''\0''

instead of

while ((c = getc(fp)) != EOF) {
*bp++ = c;
}
*bp = ''\0'';

why can''t I get it to work?

here is the full program.
i''m running gentoo/linux.

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

size_t bufsize = 512;

int main(int argc, char *argv[])
{
FILE *fp;
char *bp;
int i;
int c;

*argv++;
fp = fopen(*argv, "r");

if ((bp = malloc(bufsize * sizeof(char *))) == NULL) {
printf("cannot alocate memory.\n");
return 1;
}

/*i = 0;*/
while ((c = getc(fp)) != EOF) {
*bp++ = c;
/* bp[i++] = c */
}
*bp = ''\0'';
/* bp[i] = ''\0'' */
fclose(fp);

printf("%s", bp);

free(bp);

return 0;
}

推荐答案



你的基础宝inter bp未被修改。

Your base pointer bp is not modified.



您的基本指针已被修改。

Your base pointer is modified.



你缺少一个临时指针:

char * temp;

You lack a temporary pointer:
char *temp;



不便携。

退出(EXIT_FAILURE);

是。

Not portable.
exit(EXIT_FAILURE);
is.



现在,让我们得到一个可以修改的指针:

temp = bp;

Now, let''s get a pointer that we may modify:
temp = bp;



您可以更改基指针。它不再指向

分配存储的起始地址。


这在改变内容方面也是如此。
$ b已分配存储空间的$ b但不忘记

存储的开头(存储在bp中):


* temp ++ = c;

You change your base pointer. It does not longer point to
the start address of the allocated storage.

This achieves the same with respect to changing the contents
of the allocated storage but does not "forget" the start of
the storage (which is stored in bp):

*temp++ = c;



你设置* bp =''\''',即bp代表空字符串。


* temp =''\''';

虽然temp给你空字符串,但bp会给你

完整的字符串。

You set *bp = ''\0'', i.e. bp represents the empty string.

*temp = ''\0'';
While temp gives you the empty string, bp gives you the
full string.



您的版本:您打印空字符串。

临时版本:您打印所需的字符串。

Your version: You print the empty string.
temp version: You print the desired string.



您的版本:您释放了错误的地址 - 这会导致未定义的

行为。

临时版本:一切都按预期工作。

Your version: You free the wrong address -- this leads to undefined
behaviour.
Temp version: everything works as intended.



干杯

Michael

-

电子邮箱:我的是/ at / gmx /点/地址。


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.




你的基本指针bp不是修改d。


Your base pointer bp is not modified.



您的基本指针已被修改。


Your base pointer is modified.



你缺少一个临时指针:

char * temp;


You lack a temporary pointer:
char *temp;



不便携。

退出(EXIT_FAILURE);

是。


Not portable.
exit(EXIT_FAILURE);
is.



现在,让'我们得到一个可以修改的指针:

temp = bp;


Now, let''s get a pointer that we may modify:
temp = bp;



您更改基本指针。它不再指向

分配存储的起始地址。


这在改变内容方面也是如此。
$ b已分配存储空间的$ b但不忘记

存储的开头(存储在bp中):


* temp ++ = c;


You change your base pointer. It does not longer point to
the start address of the allocated storage.

This achieves the same with respect to changing the contents
of the allocated storage but does not "forget" the start of
the storage (which is stored in bp):

*temp++ = c;



你设置* bp =''\''',即bp代表空字符串。


* temp =''\''';

虽然temp给你空字符串,但bp会给你

完整的字符串。


You set *bp = ''\0'', i.e. bp represents the empty string.

*temp = ''\0'';
While temp gives you the empty string, bp gives you the
full string.



您的版本:您打印空字符串。

临时版本:您打印所需的字符串。


Your version: You print the empty string.
temp version: You print the desired string.



您的版本:您释放了错误的地址 - 这导致未定义的

行为。

临时版本:一切都按预期工作。


Your version: You free the wrong address -- this leads to undefined
behaviour.
Temp version: everything works as intended.




干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。



Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.



谢谢

Thanks




这取消引用argv然后将argv增加1.

取消引用的值被丢弃。所以你可以

简单地写了argv ++但是我认为你不允许修改argv的值。所以你应该进一步写下

fp = fopen(*(argv + 1)," r");而不是

。但你显然还需要

检查argc 1

This dereferences argv and then increases argv by 1.
The dereferenced value gets discarded. So you could
have simply written argv++ But I think that you''re not
allowed to modify the value of argv. So instead of doing
that you should have written further down
fp = fopen(*(argv+1), "r"); But you obviously need to also
check that argc 1



你需要在这里检查返回值为NULL。

You need to check the return value here for NULL.



我怀疑你的意思是sizeof(char)在这里(这是1)相比

比sizeof(char *)

I suspect you mean sizeof(char) here (which is 1) rather
than sizeof(char *)



返回值1适用于Unix / Linux但不可移植。

我们更喜欢这里的可移植代码。

A return value of 1 works for Unix/Linux but is not portable.
We prefer portable code around here.



在循环中你需要检查你没有得到

a缓冲区溢出。

Inside the loop you need to check that you don''t get
a buffer overflow.



此时bp指向一个内存位置

包含NUL字符,而不是
$ b的开头$ b你的缓冲区。

At this point bp points to a memory location which
contains the NUL character , not to the beginning of
your buffer.


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

10-27 12:18