Why this program is crashing ???

C

code break

Can Any one tell me why this program is crashing .

testFunc()
{
int a1[10], *ptr ;

f=&a1[0];
f=f+1;
return 0;
}
 
V

Vladimir S. Oka

code said:
Can Any one tell me why this program is crashing .

NO. For why, see below.
testFunc()

int testFunc(void) /* much better style */
{
int a1[10], *ptr ;

f=&a1[0];
f=f+1;
return 0;
}

Because it's not a program? Show as a compilable minimum C program that
exhibits whatever problem you have. What you posted does not even
compile as a separate compilation unit to be later linked into a proper
program (e.g. you never define `f`).
 
J

Jaspreet

code said:
Can Any one tell me why this program is crashing .

testFunc()
{
int a1[10], *ptr ;

f=&a1[0];
f=f+1;
return 0;
}

A minimal program code please. What is f ?
 
N

Nikunj Shah

This is not crashing its working absolutely fine.

code said:
Can Any one tell me why this program is crashing .

NO. For why, see below.
testFunc()

int testFunc(void) /* much better style */
{
int a1[10], *ptr ;

f=&a1[0];
f=f+1;
return 0;
}

Because it's not a program? Show as a compilable minimum C program that
exhibits whatever problem you have. What you posted does not even
compile as a separate compilation unit to be later linked into a proper
program (e.g. you never define `f`).
 
N

Nikunj Shah

This is not crashing its working absolutely fine.

code said:
Can Any one tell me why this program is crashing .

NO. For why, see below.
testFunc()

int testFunc(void) /* much better style */
{
int a1[10], *ptr ;

f=&a1[0];
f=f+1;
return 0;
}

Because it's not a program? Show as a compilable minimum C program that
exhibits whatever problem you have. What you posted does not even
compile as a separate compilation unit to be later linked into a proper
program (e.g. you never define `f`).
 
V

Vladimir S. Oka

Nikunj said:
This is not crashing its working absolutely fine.

Please don't top post.

While you're at it, can you explain exactly how this works "absolutely
fine"?
Vladimir said:
code said:
Can Any one tell me why this program is crashing .

NO. For why, see below.
testFunc()

int testFunc(void) /* much better style */
{
int a1[10], *ptr ;

f=&a1[0];
f=f+1;
return 0;
}

Because it's not a program? Show as a compilable minimum C program that
exhibits whatever problem you have. What you posted does not even
compile as a separate compilation unit to be later linked into a proper
program (e.g. you never define `f`).
 
P

Pedro Graca

code said:
Can Any one tell me why this program is crashing .

What program. I can't see a program in your post; all I see is a
function.
testFunc()

int testFunc(void)

or

int testFunc(...) /* this looks strange. I have to review variable
argument list material */


I prefer to specify return type and parameters explicitly rather than
relying on the defaults.
{
int a1[10], *ptr ;

f=&a1[0];

There's no scope for `f' at this point (unless it's a global variable).
Assuming it's been declared as int* or void*, I'd write that as

f = a1;

because the name of an array specifies the address of its first element,
and, for a simple assignment to a pointer the redundant "&" and "[0]"
actally make it less legible for me.
f=f+1;
return 0;

ptr has been declared but has never been used. Delete its declaration.


If you really have this function in some code of yours, as what it
really does is just return 0 to the caller, I think you could make your
program much simpler by removing the function and replacing it with a
constant zero instead;

Example (where foo has been defined as a int):
if (foo == testFunc()) { /* do something */ }
/* replace last line with */
if (foo == 0) { /* do something */ }
 
K

Keith Thompson

Pedro Graca said:
code break wrote: [...]
testFunc()

int testFunc(void)

or

int testFunc(...) /* this looks strange. I have to review variable
argument list material */

A prototype with "..." requires at least one parameter before the "...".

Since testFunc doesn't use va_arg, there's no reason for it to have a
"..." in its prototype anyway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top