Pointer Question

G

greg

Hello Everyone.
Is the followinf legid c code?

int i;
int *A;
A=(int *)malloc(5*sizeof(int));
for(i=0; i<5; i++)
scanf("%d",A+i);

MTIA
 
R

Richard Bos

greg said:
Is the followinf legid c code?

int i;
int *A;
A=(int *)malloc(5*sizeof(int));
for(i=0; i<5; i++)
scanf("%d",A+i);

Yes, although the cast to malloc() is superfluous, and makes me
suspicious that you may have forgotten to #include <stdlib.h>, which
would make the code have undefined behaviour.
Also, it's badly indented, but that's probably Outhouse Express' fault.

Richard
 
P

pete

Richard said:
Yes, although the cast to malloc() is superfluous, and makes me
suspicious that you may have forgotten to #include <stdlib.h>, which
would make the code have undefined behaviour.
Also,

... it's missing a check
to make sure that A doesn't equal NULL in the scanf call.
 
C

Clunixchit

gregwrote:
Hello Everyone.
Is the followinf legid c code?

int i;
int *A;
A=(int *)malloc(5*sizeof(int));
for(i=0; i<5; i++)
scanf("%d",A+i);

MTIA

i wld propose this instead:

int i;
int *A=(int *)malloc(5*sizeof(*A));
if (!A) {
perror("malloc");
exit(-1);
}
for(i=0; i<5; i++)
scanf("%d",*(A+i));
 
K

Keith Thompson

i wld propose this instead:

int i;
int *A=(int *)malloc(5*sizeof(*A));

The cast is unnecessary and can mask errors.

Identifiers in all-caps are traditionally reserved for macros (not a
legality issue).
if (!A) {
perror("malloc");

The standard does not say that malloc() sets errno on an error. It
may do so on some systems, but then you should set errno to 0 before
the call.
exit(-1);

The only portable arguments to exit() are 0, EXIT_SUCCESS, and
EXIT_FAILURE.
 
E

Emmanuel Delahaye

greg wrote on 26/05/05 :
Hello Everyone.
Is the followinf legid c code?

If you meant 'correct C-code', no.
int i;
int *A;
A=(int *)malloc(5*sizeof(int));

A function can only be called from a function.

Lack of prototype for malloc().
for(i=0; i<5; i++)
scanf("%d",A+i);

The return of malloc() has not been tested against NULL before use...

Ok. I see what you meant and it's roughly correct, but you should have
posted a more complete code. We tend to be accurate here...

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair
 
E

Emmanuel Delahaye

Clunixchit wrote on 27/05/05 :
i wld propose this instead:

int i;
int *A=(int *)malloc(5*sizeof(*A));
if (!A) {
perror("malloc");

The C language doesn't garantee that a fail on malloc() will raise
errno.
exit(-1);

Not portable. The portable values are
0
EXIT_SUCCESS
EXIT_FAILURE

The latter are defined in said:
}
for(i=0; i<5; i++)
scanf("%d",*(A+i));

No. The original code was correct. *(A+i) is a twitsted form for A
which is undoubtly an int, according to the definition of the A
pointer...

3 adds, 1 change, 3 mistakes. Good shot!

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.
 
C

Clunixchit

[quote:6f1ee38e52]if (!A) {
perror("malloc");
The standard does not say that malloc() sets errno on an error. It
may do so on some systems, but then you should set errno to 0 before
the call.
[/quote:6f1ee38e52]

what do you mean by that?
sld i do perror("Memory allocated problem"); ?
 
K

Keith Thompson

[quote:6f1ee38e52]if (!A) {
perror("malloc");
The standard does not say that malloc() sets errno on an error. It
may do so on some systems, but then you should set errno to 0 before
the call.
[/quote:6f1ee38e52]

what do you mean by that?
sld i do perror("Memory allocated problem"); ?

What part are you having trouble understanding?

The perror() function uses the value of errno. A failing malloc() may
or may not set errno, so using perror() after a failed malloc() call
doesn't make much sense. Changing the string you pass to perror()
accomplishes nothing.

(And please fix your quoting.)
 
C

Clunixchit

thanks you have answer my question :)

another question:
knowing that program is a pointer.
should i do this? or it will exit my program automaticaller?
if (!program){
return;
}
 
K

Keith Thompson

another question:
knowing that program is a pointer.
should i do this? or it will exit my program automaticaller?
if (!program){
return;
}

I have no idea.

Assuming "program" is a pointer object, the code fragment above will
execute the return statement if and only if the current value of
"program" is a null pointer. If the name "program" is intended to
mean something, you'll have to clarify what it is; "program" is just
an ordinary identifier with no special significance. The return
statement will exit the current function. It will exit your program
only if it appears within the body of main(). If it is within main(),
it should specify a value, since main is declared to return int; the
only portable values are 0, EXIT_SUCCESS, and EXIT_FAILURE.

Without knowing the context or what you're trying to accomplish, we
have no way of guessing what you should do.
 

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
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top