且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

返回后是否更改了NULL指针?

更新时间:2023-12-01 08:20:16

fix< fi*@here.com>写道:
fix <fi*@here.com> writes:
void normalize(Fraction * fr){
int factor;
if(fr-> denominator == 0)//无效的函数
{
fr = NULL; //将其设置为NULL并返回

printf("设置fr = NULL \ n");
if(fr!= NULL)
printf(&not) null");

printf(" null");
返回;
void normalize(Fraction *fr){
int factor;
if (fr->denominator == 0) // Invalid function
{
fr = NULL; // set it to NULL and return

printf("Set fr = NULL\n");
if (fr!=NULL)
printf("Not null");
else
printf("null");
return;




这是在FAQ中。


4.8:我有一个接受的功能,应该初始化,

a指针:


void f (int * ip)

{

static int dummy = 5;

ip =& dummy;

}


但是当我这样称呼时:


int * ip;

f(ip); 调用者中的指针保持不变。


A:你确定该函数初始化了你认为它做了什么吗?

请记住,C中的参数是按值传递的。被调用的

函数只改变了指针的传递副本。你要

想要传递指针的地址(函数

最终会接受一个指向指针的指针),或者拥有

函数返回指针。


另见问题4.9和4.11。

-

char a [] =" \ n .CJacehknorstu&quot ;; int putchar(int); int main(void){unsigned long b []

= {0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa ,0xaa9aa9f6,0x1f6},* p =

b,x,i = 24; for(; p + =!* p; * p / = 4)switch(x = * p& 3)case 0 :{return 0; for(p - ; i - ; i - )case

2:{i ++; if(1)break; else default:continue; if(0)case 1 :putchar(a [i& 15]); break;}}}



This is in the FAQ.

4.8: I have a function which accepts, and is supposed to initialize,
a pointer:

void f(int *ip)
{
static int dummy = 5;
ip = &dummy;
}

But when I call it like this:

int *ip;
f(ip);

the pointer in the caller remains unchanged.

A: Are you sure the function initialized what you thought it did?
Remember that arguments in C are passed by value. The called
function altered only the passed copy of the pointer. You''ll
either want to pass the address of the pointer (the function
will end up accepting a pointer-to-a-pointer), or have the
function return the pointer.

See also questions 4.9 and 4.11.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x1f6},*p=
b,x,i=24;for(;p+=!*p;*p/=4)switch(x=*p&3)case 0:{return 0;for(p--;i--;i--)case
2:{i++;if(1)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}




#void normalize(Fraction * fr){


这会将Fraction *参数值复制到局部变量fr中。

对fr本身的所有更改都是对这个局部变量而不是传播

回到来电者。它是对* fr(或fr-> ...)的变化,对于来电者来说是可见的




您可以改为做某事喜欢

分数*标准化(分数* fr){

...

返回fr;

}

随着函数的产生而将更改传播回调用者。

然后你可以调用

frp = normalize(frp);

if(frp!= NULL)

printf(" Not null");

else

printf(" null" ;);


#frp =& fr;

#normalize(frp);


此通过结构的地址,而不是指针的地址

变量到结构。


-

Derk Gwen http://derkgwen.250free.com/html/index.html

我希望对你来说感觉很好。没有什么比这更好的了吗?b $ b,他们是否有其他人的缺点,是吗?

# void normalize(Fraction *fr){

This copies the Fraction* parameter value into a local variable fr.
All changes to fr itself are to this local variable and not propagated
back to the caller. It''s changes to *fr (or fr->...) that are visible
to the caller.

You can instead do something like
Fraction *normalize(Fraction *fr) {
...
return fr;
}
which propagates changes back to the caller as the function yield.
You can then call
frp = normalize(frp);
if (frp!=NULL)
printf("Not null");
else
printf("null");

# frp = &fr;
# normalize(frp);

This passes the address of the structure, not the address of pointer
variable to the structure.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
I hope it feels so good to be right. There''s nothing more
exhilirating pointing out the shortcomings of others, is there?


在''comp.lang.c'' ,修复< fi*@here.com>写道:
In ''comp.lang.c'', fix <fi*@here.com> wrote:
Fraction.c:
#include" Fraction.h"
Fraction.c:
#include "Fraction.h"



< snipped>


首先,我强烈建议您更改代码组织。将代码包含在.h中是一个非常糟糕的主意。

。我推荐一个非常常见的

方法如下:


/ * main.c * /

#include" Fraction。 h"

#include< stdio.h>


/ * macros ================= ================================= ============ * /

/ *常数========================================== ======== ========= * /

/ * types ==================== ============================== ============= * /

/ *结构============================================ ====== ======== * /

/ *私有变量====================== ============================ = * /

/ *私人功能======= =========================================== = * /


静态无效Case1c(无效)

{

printFraction(newFraction(0,4));

printf (" \ n");

printFraction(newFraction(4,0));

printf(" \ n");

printFraction(newFraction(0,0));

pri ntf(" \ n");

}


/ *入口点=============== =================================== ======= * /


int main(无效)

{

printf(" Case 1c:\ n");

Case1c ();

返回0;

}


/ *公共变量=========== ======================================= == * /
>
/ * fraction.h * /

#ifndef H_FRACTION

#define H_FRACTION

/ * macros ===== ============================================= ===== ======= * /

/ *常数============================== ==================== ========= * /

/ * types ======== ========================================== ======== ===== * /

/ *结构================================ ================== ======== * /


typedef struct

{

int sign;

int numerator;

int denominator;

}

分数;


/ *内部公共功能======================================= ==== * /

/ *入口点================================ ================== ====== * /


分数newFraction(int num,int den);

void printFraction(Fraction fr);


/ * public variables ===================== ============================= == * /


#endif / *警卫* /


/ * fraction.c * /

#include< stdio.h>

#include" fraction .h"


/ * macros ================================ ================== ============ * /

/ *常数======= =========================================== ======= == * /

/ * types =================================== =============== ============= * /

/ *结构========= ========================================= ======== * /

/ *私有变量===================================== ============= = * /

/ *私人福nctions ================================================= = = * /


static int signOf(int number)

{

if(number< 0)

返回-1;

其他

返回1;

}
>
static int gcd(int num1,int num2)

{

if(num1< num2)

返回gcd( num2,num1);

else if((num1%num2)== 0)

返回num2;

else

返回gcd(num2,num1%num2);

}


static void normalize(分数* fr)

{

int factor;

if(fr-> denominator == 0)/ *函数无效* /

{

fr = NULL; / *将其设置为NULL并返回* /


printf(设置fr = NULL \ n);

if(fr!= NULL )

printf(非空);

其他

printf(" null");

返回;

}

/ *从符号变量的分子和分母处取符号

* /

fr-> sign * = signOf(fr-> numerator);

fr-> sign * = signOf(fr-> denominator);


/ *绝对分子和分母* /

fr->分子* = signOf(fr->分子);

fr->分母* = signOf(fr->分母);


if(fr->分子== 0)

{/ *如果分子为0 ,简化为0/1

* /

fr->分母= 1;

fr-> sign = 1;

}

其他

{/ *找到分子的gcd和

分母,用gcd除以它们* /

factor = gcd(fr->分子,fr->分母);

fr-> numerator / = factor;

fr-> denominator / = factor;

}

}


/ *内部公共函数====== ===================================== * /

/ *条目积分================================================= = ====== * /


分数newFraction(int num,int den)

{

分数fr;

分数* frp;


fr.numerator = num;

fr.denominator = den;

fr.sign = 1;

frp =& fr;

normalize(frp);

if(frp!= NULL)

printf(" Not null");

else

printf(" null");


返回fr;

}


无效printFraction(分数fr)

{

if(& fr == NULL)

printf(无效分数);


if(fr.sign == -1)

printf(" - && ;);

printf("%i /%i",fr.numerator,fr.denominator);

}


/ *公共变量=========================================== ======= == * /


请注意,我没有更改您的代码。


-

-ed- em**********@noos.fr [在回答我之前删除YOURBRA]

C语言常见问题解答: http://www.eskimo.com/~scs/C-faq/top.html

C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=cpp

FAQ de fclc: http://www.isty-info.uvsq.fr/~rumeau/fclc/


<snipped>

First of all, I highly recommend that you change your code organization. It''s
a very bad idea to have code included in a .h. I suggest a very common
approach as follow:

/* main.c */
#include "Fraction.h"
#include <stdio.h>

/* macros ================================================== ============ */
/* constants ================================================== ========= */
/* types ================================================== ============= */
/* structures ================================================== ======== */
/* private variables ================================================== = */
/* private functions ================================================== = */

static void Case1c (void)
{
printFraction (newFraction (0, 4));
printf ("\n");
printFraction (newFraction (4, 0));
printf ("\n");
printFraction (newFraction (0, 0));
printf ("\n");
}

/* entry point ================================================== ======= */

int main (void)
{
printf ("Case 1c:\n");
Case1c ();
return 0;
}

/* public variables ================================================== == */

/* fraction.h */
#ifndef H_FRACTION
#define H_FRACTION
/* macros ================================================== ============ */
/* constants ================================================== ========= */
/* types ================================================== ============= */
/* structures ================================================== ======== */

typedef struct
{
int sign;
int numerator;
int denominator;
}
Fraction;

/* internal public functions =========================================== */
/* entry points ================================================== ====== */

Fraction newFraction (int num, int den);
void printFraction (Fraction fr);

/* public variables ================================================== == */

#endif /* guard */

/* fraction.c */
#include <stdio.h>
#include "fraction.h"

/* macros ================================================== ============ */
/* constants ================================================== ========= */
/* types ================================================== ============= */
/* structures ================================================== ======== */
/* private variables ================================================== = */
/* private functions ================================================== = */

static int signOf (int number)
{
if (number < 0)
return -1;
else
return 1;
}

static int gcd (int num1, int num2)
{
if (num1 < num2)
return gcd (num2, num1);
else if ((num1 % num2) == 0)
return num2;
else
return gcd (num2, num1 % num2);
}

static void normalize (Fraction * fr)
{
int factor;
if (fr->denominator == 0) /* Invalid function */
{
fr = NULL; /* set it to NULL and return */

printf ("Set fr = NULL\n");
if (fr != NULL)
printf ("Not null");
else
printf ("null");
return;
}
/* Take the signs form the numerator and denominator to the sign variable
*/
fr->sign *= signOf (fr->numerator);
fr->sign *= signOf (fr->denominator);

/* Absolute the numerator and denominator */
fr->numerator *= signOf (fr->numerator);
fr->denominator *= signOf (fr->denominator);

if (fr->numerator == 0)
{ /* If the numerator is 0, simplify it to 0/1
*/
fr->denominator = 1;
fr->sign = 1;
}
else
{ /* Find the gcd of the numerator and
denominator, divide them by the gcd */
factor = gcd (fr->numerator, fr->denominator);
fr->numerator /= factor;
fr->denominator /= factor;
}
}

/* internal public functions =========================================== */
/* entry points ================================================== ====== */

Fraction newFraction (int num, int den)
{
Fraction fr;
Fraction *frp;

fr.numerator = num;
fr.denominator = den;
fr.sign = 1;
frp = &fr;
normalize (frp);
if (frp != NULL)
printf ("Not null");
else
printf ("null");

return fr;
}

void printFraction (Fraction fr)
{
if (&fr == NULL)
printf ("Invalid fraction");

if (fr.sign == -1)
printf ("-");
printf ("%i/%i", fr.numerator, fr.denominator);
}

/* public variables ================================================== == */

Note that I have no changed your code.

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=cpp
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/