Solved

dynamic array

Posted on 2002-07-15
9
211 Views
Last Modified: 2012-05-04
How to create a dynamic array, with the array size is determine by user input?
Pls give a simple example.Thanx you.
0
Comment
Question by:exiang
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
ID: 7156178
#include <stdio.h>
main(){
    int arraysize;
    int *array;
    int i;
    scanf("%d",&arraysize);
    array = (int *)malloc(sizeof(*array)*arraysize);
    for( i=0; i<arraysize; i++ ){
          scanf("%d",&array[i]);
    }
    for( i=0; i<arraysize; i++ ){
          printf("%d\n",array[i]);
    }
}
0
 
LVL 3

Expert Comment

by:cincin77
ID: 7158967
int he malloc line;
instead of using sizeof(* array) you should use sizeof(int)
as the elements of the array are integers. However for this specifiz example, sizeof(* array) should be equal to 4 which is equal to sizeo(int) :))

regards...
0
 

Expert Comment

by:Theril
ID: 7159913
> instead of using sizeof(* array) you should use sizeof(int)
as the elements of the array are integers. However for this specifiz example, sizeof(* array) should be equal to 4 which is equal to sizeo(int) :))

No, *array points to an int, so the effect is exactly same as sizeof(int). You propably mixed it with sizeof(array)?
Using *array insteads of *int is propably wiser, as there may come situation where one changes the datatype of array.
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 3

Expert Comment

by:cincin77
ID: 7160328
array itself is a pointer and * array is a pointer to a pointer whose size is machine dependent.

do i miss something?
0
 

Expert Comment

by:Theril
ID: 7160963
> array itself is a pointer and * array is a pointer to a pointer whose size is machine dependent.

Maybe I said "*array points to an int" a bit wrong. What I ment was "*array is an int". Elements of int array are ints and *array is first element of the array (array[0]), so it's size is sizeof(int).

Try this one:

#include <stdio.h>
int main()
{
     char *foo = "foo";
     printf("Sizes: foo: %i *foo: %i\n", sizeof(foo), sizeof(*foo));
}
0
 
LVL 3

Expert Comment

by:cincin77
ID: 7161371
iygggg
what i said was really ridiculous, i wrote as if array is a type.
sorry for this mistake.

thanks theril...

0
 
LVL 2

Expert Comment

by:jonnin
ID: 7161859
note that you can resize the array with realloc; this is slow (do not add one element at a time, add 100 or 1000 or something each time you run out of space)

0
 

Author Comment

by:exiang
ID: 7170987
should include a header file called <alloc.h>
0
 
LVL 84

Expert Comment

by:ozo
ID: 7171022
<alloc.h> is not ANSI standard
should include a header file called <stdlib.h>
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
In C, why can't I shift a 64-bit integer more than 32 bits? 13 223
why "." vs "->" 23 120
Super Scope, DHCP 5 77
What is sub-make ? 2 60
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question