Dynamic Array

S

suriya

chai said:
Can anyone help me in finding elements stored in a dynamic array in.

Hi Dear,

U check this code. It is allocating required memory dynamically,
checking for allocation failure, storing the values and retrieving back
and printing them. Finally deallocating the memory. Let me know is this
the answer to u question.

#include<stdio.h>
#include<malloc.h>
int main()
{
int *p;
int n,i,num;

printf("enter num of elements");
scanf("%d",&n);

p= (int*)malloc(n*sizeof(int));

if(p ==NULL)
{
printf("Dynamic allocation failed");
return 1;
}

for(i=0;i<n;i++)
{
printf("enter number");
scanf("%d",&num);
*(p+i)= num;
}

printf(" \n The elements are ");
for(i=0;i<n;i++)
{
printf(" %d", *(p+i));
}

free(p);
return 0;
}
/* end */
 
S

suriya

chai said:
Can anyone help me in finding elements stored in a dynamic array in.

Hi Dear,

U check this code. It is allocating required memory dynamically,
checking for allocation failure, storing the values and retrieving back
and printing them. Finally deallocating the memory. Let me know is this
the answer to u question.

#include<stdio.h>
#include<malloc.h>
int main()
{
int *p;
int n,i,num;

printf("enter num of elements");
scanf("%d",&n);

p= (int*)malloc(n*sizeof(int));

if(p ==NULL)
{
printf("Dynamic allocation failed");
return 1;
}

for(i=0;i<n;i++)
{
printf("enter number");
scanf("%d",&num);
*(p+i)= num;
}

printf(" \n The elements are ");
for(i=0;i<n;i++)
{
printf(" %d", *(p+i));
}

free(p);
return 0;
}
/* end */
 
A

Alexei A. Frounze

chai said:
Can anyone help me in finding elements stored in a dynamic array in.


This group deals with standard C, not algorithms. If you have an algorithm
but have problems expressing it's in terms of C (your code isn't compiling
or working as per the algorithm), come here for the help. But if you need
the algorithm, this isn't the right group to ask for it.

Alex
 
M

Mabden

suriya said:
Hi Dear,

U check this code. It is allocating required memory dynamically,
checking for allocation failure, storing the values and retrieving back
and printing them. Finally deallocating the memory. Let me know is this
the answer to u question.

#include<stdio.h>
#include<malloc.h>
int main()
{
int *p;
int n,i,num;

printf("enter num of elements");
scanf("%d",&n);

p= (int*)malloc(n*sizeof(int));

if(p ==NULL)
{
printf("Dynamic allocation failed");
return 1;
}

for(i=0;i<n;i++)
{
printf("enter number");
scanf("%d",&num);
*(p+i)= num;
}

printf(" \n The elements are ");
for(i=0;i<n;i++)
{
printf(" %d", *(p+i));
}

free(p);
return 0;
}
/* end */

Three times is the charm.
Hi Dear,

U check this code. It is allocating required memory dynamically,
checking for allocation failure, storing the values and retrieving back
and printing them. Finally deallocating the memory. Let me know is this
the answer to u question.

#include<stdio.h>
#include<malloc.h>
int main()
{
int *p;
int n,i,num;

printf("enter num of elements");
scanf("%d",&n);

p= (int*)malloc(n*sizeof(int));

if(p ==NULL)
{
printf("Dynamic allocation failed");
return 1;
}

for(i=0;i<n;i++)
{
printf("enter number");
scanf("%d",&num);
*(p+i)= num;
}

printf(" \n The elements are ");
for(i=0;i<n;i++)
{
printf(" %d", *(p+i));
}

free(p);
return 0;
}
/* end */
 
F

Flash Gordon

You should read the comp.lang.c FAQ which has lots of information on
pointers and dynamic memory. Also, a better statement of your problem
including a small program showing it would allow us to help you rather more.
Hi Dear,

U check this code. It is allocating required memory dynamically,

Please don't use abbreviations like "u". All they do is make your post
harder to read. Remember that many more people read your post than write
it, so it is worth a few extra keystrokes.
checking for allocation failure, storing the values and retrieving back
and printing them. Finally deallocating the memory. Let me know is this
the answer to u question.

#include<stdio.h>

The space shortage was resolved years ago, so why not use spaces to make
your code easier to read?
#include said:
#include<malloc.h>

There is no such header as malloc.h in standard C, although your
implementation might provide one. Use the standard stdlib.h header instead.
#include said:
int main()

It is better to specify that you are not using parameters.
int main(void)
{
int *p;
int n,i,num;

printf("enter num of elements");
scanf("%d",&n);

You should check to see if scanf succeeded. What if the user typed in "ten"?
p= (int*)malloc(n*sizeof(int));

There is no need to cast the result of malloc and doing so can hide not
having a correct prototype in scope which is a bad thing. Also, you can
use the sizeof the object being pointed at rather than the type to
simplify maintenance.

p = malloc(n * sizeof *p);
if(p ==NULL)
{
printf("Dynamic allocation failed");
return 1;

This is a non-standard return value from main. Unless you have reason
not to you should use 0, EXIT_SUCCESS or EXIT_FAILURE with the latter
two defined in stdlib.h. On some systems returning 1 will signal success!
return EXIT_FAILURE;
}

for(i=0;i<n;i++)
{
printf("enter number");

This may not be displayed before waiting for input since stdout is
likely to be line buffered. To maximise your chances you should:
fflush(stdout);
scanf("%d",&num);

Again you need to check whether scanf succeeded and deal with failure.
*(p+i)= num;

A simpler expression is
p = num;
}

printf(" \n The elements are ");
for(i=0;i<n;i++)
{
printf(" %d", *(p+i));

This can again be simplified to
printf(" %d", *p);

To ensure the last line is visible you need to finish with a newline.
putchar('\n');
 
M

Martin Ambuhl

Mabden said:
Three times is the charm.
Hi Dear,

U check this code.

By the third time, I would think that you could have learned to spell
such simple words as "you" and "your". Script-kiddies rule, indeed.
 
M

Mabden

Martin Ambuhl said:
By the third time, I would think that you could have learned to spell
such simple words as "you" and "your". Script-kiddies rule, indeed.

I assume you are speaking to suriya the OP...

You see when they post the same text over and over, they don't actually
change the content, otherwise it wouldn't be "the same text". I was just
hurrying the process along.

But thank you for playing.
 
M

Martin Ambuhl

Mabden said:
I assume you are speaking to suriya the OP...

And to you, who reposted the illiteracies without quotation. You have
committed a large number of sins:
1) Posting those illiteracies
2) Plagiarism
and ...
You see when they post the same text over and over, they don't actually
change the content, otherwise it wouldn't be "the same text". I was just
hurrying the process along.

But thank you for playing.

3) Pretending that your posting others words as your own should be read
as a failure on the reader's part.

No, I won't thank you for playing. Your double dishonesty should not be
thanked, nor should your idiocy.
 
M

Mabden

Martin Ambuhl said:
And to you, who reposted the illiteracies without quotation. You have
committed a large number of sins:
1) Posting those illiteracies
2) Plagiarism
and ...

Without quotation? Isn't what the indenting is? I don't see quotes
around what you posted... you plagiarismist (I assume you'll correct
that for me if you quote it). Oh, and you forgot to fix the text, just
as I did - shame on us both for our sins. Why would you post something
that some else wrote without fixing all of their mistakes? Oh, yeah,
because that's not what one does when quoting! Idiot.
3) Pretending that your posting others words as your own should be read
as a failure on the reader's part.

Well, if you didn't take the time to read it, why are you posting angry
messages into the Eternal Record of the Internet. It could make you look
the fool.
No, I won't thank you for playing. Your double dishonesty should not be
thanked, nor should your idiocy.

I wrote all of the text in this message that is not prefaced with
indentation characters. Does that make it clear to you? I wouldn't want
another misunderstanding on your part to make me seem dishonest in your
eyes. In fact, I would like to go on record as saying I have and will
always preface quotations with some indentation characters. In the
spirit of honesty and the fact that that's the way it works.

My idiocy is also on record, nor are you the first to notice. Nor are
you the last.

And thanks for playing, again. Three times is, of course, the charm, as
stated above, so I expect an even longer rant about some meaningless
lesson on honesty and textual invention in the future. Please try to add
a Haiku to Array your anger and indignity with more, oh let's say,
Dynamics. Regards.
 
S

suriya

Hi Flash,
Please don't use abbreviations like "u". All they do is make your post
harder to read. Remember that many more people read your post than write
it, so it is worth a few extra keystrokes.


The space shortage was resolved years ago, so why not use spaces to make
your code easier to read?
#include <stdio.h>
It is better to specify that you are not using parameters.
int main(void)

Thanks for these tips



p= (int*)malloc(n*sizeof(int));
you should note that malloc returns void* and it is better to typecast
the return value.


A simpler expression is
p = num; Good.

This can again be simplified to

printf(" %d", *p);

This won't work as p is pointer. It should be *(p+i) or only p.

All said and done. I just want to give quick idea about dynamic array.
I appreciate your keen interest in analysing code line by line.
Thanks for giving your valuable suggestions

Also I apologizes to this group for sending same reply three times. It
just happened that I was refreshing my web page and that mail was sent
thrice. I am sorry for that.
Suriya
 
K

Keith Thompson

suriya said:
p= (int*)malloc(n*sizeof(int));
you should note that malloc returns void* and it is better to typecast
the return value.

No, it really isn't. Yes, malloc returns void*, but an expression of
type void* can be implicitly converted to any pointer-to-object type.
The cast is unnecessary, and it can hide certain errors, such as a
failure to "#include <stdlib.h>". Flash Gordon explained this in text
that you didn't quote.

The best way to do the allocation is

p = malloc(n * sizeof *p);
 
F

Flash Gordon

suriya wrote:

p= (int*)malloc(n*sizeof(int));
you should note that malloc returns void* and it is better to typecast
the return value.

Yes, it returns a void*, but no, it is definitely not better to cast it
(unless your initials are PJP). The return value will be converted
without the cast, and casting would remove the warning the compiler is
otherwise *required* to generate if you fail to include stdlib.h

Also, by doing it the way you show you have to get the type correct in
three places, and change it in three places if the type changes. The
generally recommended method here is
p = malloc(n * sizeof *p);
and this is for very good reasons which have been debated many times in
the past.
A simpler expression is
p = num;
Good.


This can again be simplified to


printf(" %d", *p);

This won't work as p is pointer. It should be *(p+i) or only p.


<snip>

That was a typo (or rather an editing error), I had intended p.
 
M

Martin Ambuhl

suriya said:
p= (int*)malloc(n*sizeof(int));
you should note that malloc returns void* and it is better to typecast
the return value.

BZZT! Thanks for playing. Please direct such incorrect comments to
comp.lang.c++, where their language has saddled them with such silly rules.
And 'sizeof(int)' is at best a stylistic error. Try:
p = malloc(n * sizeof *p);
 
G

Gregory Pietsch

chai said:
Can anyone help me in finding elements stored in a dynamic array in.

If you are asking about dynamic array code, just grab FreeDOS Edlin
from either ibiblio or alt.sources and marvel at the dynarray.h file,
which contains an implementation of dynamic arrays that can be used for
just about any type.

Gregory Pietsch
 

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

Forum statistics

Threads
474,170
Messages
2,570,924
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top