C
chai
Can anyone help me in finding elements stored in a dynamic array in.
chai said:Can anyone help me in finding elements stored in a dynamic array in.
chai said:Can anyone help me in finding elements stored in a dynamic array in.
chai said:Can anyone help me in finding elements stored in a dynamic array in.
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 */
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 said:#include<malloc.h>
#include said: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));
Mabden said:Three times is the charm.
Hi Dear,
U check this code.
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.
Mabden said: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.
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 ...
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.
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)
p = num; Good.
This can again be simplified to
suriya said:p= (int*)malloc(n*sizeof(int));
you should note that malloc returns void* and it is better to typecast
the return value.
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.
suriya said:p= (int*)malloc(n*sizeof(int));
you should note that malloc returns void* and it is better to typecast
the return value.
chai said:Can anyone help me in finding elements stored in a dynamic array in.
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.