由买买提看人间百态

topics

全部话题 - 话题: fun2
1 (共1页)
G****u
发帖数: 51
1
有下面这段code, 为什么fun1()输出乱码, 而fun2()输出正确: ABC.
====== code ============
#include
int main() {
char *fun1(), *fun2();
printf("%s\n%s\n", fun1(), fun2());
return 0;
}
char *fun1() {
char str[]="ABC";
return (str);
}
char *fun2() {
char (*str)[]="ABC";
return (str);
}
p*********t
发帖数: 2690
2
fun1()在返回一个局部变量的地址,所以输出的是這個地址的值,因为fun1()的局部变
量在函数结束时消失,那个地址可能又被分配了新的值,所以是乱码。
要想输出abc,可以在fun1()的str设为static,这样即使fun1()函数结束,str[]还是在
内存存在。
char *fun1() {
static char str[]="ABC";
return (str);
}

有下面这段code, 为什么fun1()输出乱码, 而fun2()输出正确: ABC.
====== code ============
#include
int main() {
char *fun1(), *fun2();
printf("%s\n%s\n", fun1(), fun2());
return 0;
}
char *fun1() {
char str[]="ABC";
return (str);
}
char *fun2() {
char (*str)[]="ABC";
return (str);
... 阅读全帖
l*******h
发帖数: 3
3
来自主题: Programming版 - C/C++函数调用和栈内存
这几天学C语言学晕了,都因是看内存管理变量分配的问题.现在看RETURN语句都不顺眼.
比如下面第一个CODE,因为B指针指向栈内存,离开fun2()就失效,所以返回main()的结果
不正确.
int *fun2(void)
{
int b=2;
return &b;
}
main()
{
int *b1=fun2();
printf(“%\n”, *b1);
}
那下面的CODE就对了吗? 变量b在fun1()子程序里不也是在栈内存吗?它离开fun1()子程
序不也无效了吗?怎能指望返回MAIN正确的值呢? RETURN 这个命令是怎么和把什么返回
主函数的?
int fun1(void)
{
int b=2;
return b;
}
main()
{
int b1=fun1();
printf(“%\n”, b1);
}
G****u
发帖数: 51
4
I think it does matter what str points to. If it points to something in
function stack (destroyed after function exits), then return (str) is wrong,
just like what fun1 does. However, if it points to something that has a
storage duration throughout the lifetime of program, then it is correct,
just like char *str = "ABC".
This is why I cannot understand your explanation of why fun2 is correct. yes
,
the caller get the value (the address to something) of str, but if something
is stored in functi... 阅读全帖
G****u
发帖数: 51
5
sorry, my bad, i want to say "your explanation of why fun2 is correct".
corrected in my post.
I am not arguing here, I just want to understand why fun2 is correct (or why
it is wrong if it is). I have spent hours on this.
t****t
发帖数: 6806
6
str in fun2(), of course.

fun2
b***i
发帖数: 3043
7
代码写得对不对还得看你的目的,
你觉得fun2是对的,因为运行结果正常,那么你fun2的目的是什么?
b******s
发帖数: 345
8
运行出现的warning是"starting a module while inside s do group" "finish a
module while inside a do group", 该怎样改正这段出现问题的程序呢,谢谢!下面
是出现问题的部分程序。
do i = 1 to n;
if npf > 3 then do;
WFI = XF[i,3:npf-1];
end;
else WFI = 0;
if npu > 2 then do;
WUI = Z[i,3:npu];
end;
else WUI = 0;
lbyw = byw * WFI`;
lbuw = buw * WUI`;
mux1 = bu0 + bux + lbuw;
mux0 = bu0 + lbuw;
start fun1(u) global(by0, byx, lbyw, byu, mux1);
v = (1/(1+exp(-(by0 + byx + lbyw + byu*u ))))/sqrt(2*constant('pi'))
* exp((-1/2)*(... 阅读全帖
D*******a
发帖数: 3688
9
来自主题: Programming版 - 弱问mcc和mex的区别
你没装吧
>> mcc -?
MCC Invoke MATLAB to C/C++ Compiler (Version 4.7).
MCC [-options] fun [fun2 ...]

Prepare fun.m for deployment outside of the MATLAB environment.
Generate wrapper files in C or C++ and optionally build standalone
binary files.

Write any resulting files into the current directory, by default.
...
h**********c
发帖数: 4120
10
来自主题: Programming版 - ask Big Niu industrial code 都是怎麼寫的
These things are useful quite placebo.
Fundamental I believe is still design.
Let's talk about pass by ref in Java. You encapsulate something in a class.
This class should take care of this member variable. Ok, they just pass by
ref, get the ref, modify change them even new a new one in another class and
set it back.
Cross call function. lets say a.fun1() call b.fun2(), .... finally z.fun26()
call a.fun1(). They dont' even know what is called dependency graph.In fact
, they know, they just don't... 阅读全帖
G****u
发帖数: 51
11
如果你把两个fun()分开, 各写一个main(), 每个main里printf一个fun, 那么gcc里面
都能输出ABC, 但是同样的两个程序, 放到一些online c compiler里面, main+fun1 还
是输出garbage, main+fun2 输出ABC
G****u
发帖数: 51
12
问题是为啥fun2 总能输出ABC,而fun1时而能输出ABC(gcc), 时而输出garbage? array
在C里面到底是怎么给造出来的, 一下子可以等同与pointer, 一下子不能, 这背后的原
因是啥.

ANY
t****t
发帖数: 6806
13
fun2没有错, 所以总能输出ABC, 虽然你把好好的函数写成了一滩那什么.
fun1有错, 能不能输出ABC要看天意.
至于array和pointer的关系, 请仔细阅读C FAQ第6章.
G****u
发帖数: 51
14
那fun3里的return (p) 和 fun2里的 return(str) 啥区别? gcc没有报错也没有
warning
c*******y
发帖数: 1630
15
fun3=fun1
fun2=
char **str="abc";
return str;
t****t
发帖数: 6806
16
i didn't say fun2 is wrong, i said it's poorly written. dunno what you are
arguing.
in my previous post, i said as long as it's a pointer it should be ok
(albeit bad style). if it points to something in stack, it won't be
a pointer, it will be an array. an array is never a pointer.

wrong,
,
something
is
G****u
发帖数: 51
17

if it points to something in stack, it won't be
You mean pointer cannot point to something in stack or you mean str in fun2
is not point to something in stack?
G****u
发帖数: 51
18
yes, that is what I thought. If fun2 is correct, the only explanation would
be str points to string literal. I think char (*str)[] just does the same
thing as char *str="ABC".
thanks.

't
r***t
发帖数: 21
19
in fun1, str is a local char array which has "ABC", the space is local stack.
in fun2, str is a poiner, "ABC" is treated as constant string, which will be
allocated from global space.
B********r
发帖数: 397
20

would
In fun1, str is array name of "ABC". When you return an array name whose
content was destroyed after returning, you can't get a right result.
In fun2, str is a pointer which points to an array "ABC". Since "ABC" is
constant and stays in the memory and what you return is the actual address,
you can find "ABC" in main(). (*str)[] just different from *str[] as a
pointer, instead of a pointer array.
a***d
发帖数: 336
21
来自主题: Statistics版 - R同名函数问题
this is what I usually do. there are probably much better ways of doing it.
your function is usually in .globalEnv unless you specify otherwise.
so to get your function, use:
fun1 <- get(funcNm, env=.GlobalEnv)
use search() to find out position of the package. if the package is the nth
package in the search path, use:
fun2 <- get(funcNm, pos=n)
1 (共1页)