Hello everyone,
Here's my code for making a dynamic array(the user inputs the size of the array). It's working fine but I think that I've not made the best code. Is there any suggestion for improvement or optimization?

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
	int *p;
	int n,i;
	printf("how many integers do u wanna enter\n");
	scanf("%d",&n);
	p=malloc(n*sizeof(int));
	for(i=0;i<n;i++)
	{
		printf("enter the %d element",i+1);
		scanf("%d",p);
		p++;
	}
	p=p-n;
	for(i=0;i<n;i++)
	{
		printf("%d\n",*(p+i));
	}
	free(p);
}

Hello everyone,
Here's my code for making a dynamic array(the user inputs the size of the array). It's working fine but I think that I've not made the best code. Is there any suggestion for improvement or optimization?

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
	int *p;
	int n,i;
	printf("how many integers do u wanna enter\n");
	scanf("%d",&n);
	p=malloc(n*sizeof(int));
	for(i=0;i<n;i++)
	{
		printf("enter the %d element",i+1);
		scanf("%d",p);
		p++;
	}
	p=p-n;
	for(i=0;i<n;i++)
	{
		printf("%d\n",*(p+i));
	}
	free(p);
}

No the code looks fine..If you want to optimizes try experimenting with the compiler's optimizing settings...

Also you may want to look up VLA or 'variable length arrays'

>Is there any suggestion for improvement
Yes, I can recommend improvements:

>scanf("%d",&n);
Always check your input functions for success. Especially with user input, and more especially with functions that are being used inappropriately (like scanf for user input), you need to take care that it read what you wanted. You can also use that opportunity to make the input more friendly and robust:

#include <stdio.h>
#include <stdlib.h>

int main ( void )
{
  int n;

  {
    int tries = 3;
    int extra_ch;

    printf ( "Number of integers? " );
    fflush ( stdout );

    while ( scanf ( "%d", &n ) != 1 ) {
      /* Give up after too many tries */
      if ( --tries == 0 )
        break;

      /* Notify user of the error */
      printf ( "Invalid input. Please try again: " );
      fflush ( stdout );

      /* Tidy up for the next attempt */
      clearerr ( stdin );

      do
        extra_ch = getchar();
      while ( extra_ch != '\n' && extra_ch != EOF );
    }

    if ( tries == 0 ) {
      printf ( "Too many failures. Bailing...\n" );
      return EXIT_FAILURE;
    }
  }

  printf ( "You entered %d\n", n );

  return EXIT_SUCCESS;
}

>p=malloc(n*sizeof(int));
First and foremost, malloc can return a null pointer. You need to check for that. Second, there's a way to use malloc without relying on the type of p:

p = malloc ( n * sizeof *p );

if ( p == NULL )
  panic();

The nice thing about the sizeof *p trick is that now you don't have to worry about figuring out the correct type if you have something convoluted.

>printf("enter the %d element",i+1);
This prompt is not guaranteed to show up unless you force stdout to flush itself. I haven't seen that problem on Windows, but I have seen it on Linux and Unix, so consider that as a portability concern. It's trivial to fix though:

printf ( "enter the %d element", i + 1 );
fflush ( stdout );

>}
You neglected to return a value from main. This is legal in C99, but undefined behavior in C89. It's a good idea to always return from main, for portability and correctness across the board.

>or optimization?
Your program is I/O bound and the time complexity can't be improved with a different algorithm. Any optimizations you make will be negligible in terms of code execution and completely pointless in terms of perceived performance.

This article has been dead for over six months. Start a new discussion instead.