Defining arrays of variable(user specified) length

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jun 2008
Posts: 53
Reputation: grvs is an unknown quantity at this point 
Solved Threads: 0
grvs grvs is offline Offline
Junior Poster in Training

Defining arrays of variable(user specified) length

 
1
  #1
Jun 14th, 2008
In C,how to define an array which is of a length specified by the user?
I tried writing a code, but outputs were strange. Sometimes first two elements are correctly displayed, sometimes only first. Other seems some random values (extremely large sometimes). May be the problem is in initializing the pointer. First I didn't use the pointer p in readArray() function. I don't really know why I am using it now, or whether there should be a separate pointer when &A[0] is same as A.

here is the code

  1. #include <stdio.h>
  2.  
  3. void printArray(int arr [], int size);
  4. int * readArray(int size);
  5.  
  6. int main(){
  7. int size; // size of array to be given by user
  8. printf("Enter the size of the array ");
  9. scanf("%d",&size);
  10. printArray(readArray(size),size);
  11. getchar();
  12. return 0;
  13. }
  14.  
  15. int * readArray(int size){
  16. int A [size], i, *p;
  17. p=&A[0];
  18. for (i=0;i<size;i++){
  19. printf("\nEnter the element no. %d: ",i+1);
  20. scanf("%d",(p+i));
  21. A[i]=*(p+i);
  22. }
  23. return &A[0];
  24. }
  25.  
  26. void printArray(int *arr, int size){
  27. int i=0;
  28. for (i=0; i<size;i++){
  29. printf("%d ", *(arr++));
  30. }
  31. printf("\n");
  32. getchar();
  33. }

I searched and couldn't get a solution. I got some things for C++ like this but not for C. Is there anything like new in C? (new allocates memory in C++ and java, how its different from malloc?)

PS: This is my first post in this forum, though I went through posting instruction, tell me if I should keep something in mind. Also I have done coding in Java for like 1 year, so don't hesitate to include complex solution, but as I am beginner in C/C++. I read first five chapters of "the complete reference C++, fourth edition" by Herbert Schildt (i.e. I think I understand basics of arrays and pointers) if thats useful anywhere to answer this question.

Thanks and regards
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Defining arrays of variable(user specified) length

 
1
  #2
Jun 14th, 2008
> but as I am beginner in C/C++
Unfortunately, reading schildt books won't clarify the issue either.

Pick a language and stick to it. Despite their recent common ancestry, and superficial similarity, using both of them well requires a C++ hat AND a C hat. C/C++ is just brain-damage.

> new allocates memory in C++ and java, how its different from malloc?
new calls an appropriate constructor, and malloc just gives you a block of uninitialised memory.

So in your case, you would need
int *A = malloc ( size * sizeof *A );
> scanf("%d",(p+i));
You don't need p, just do &A[i]
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 53
Reputation: grvs is an unknown quantity at this point 
Solved Threads: 0
grvs grvs is offline Offline
Junior Poster in Training

Re: Defining arrays of variable(user specified) length

 
0
  #3
Jun 14th, 2008
>
  1. int *A = malloc ( size * sizeof *A );

yups that took the input from user and displayed the output correctly, but if I am right no array was created. Just an integer pointer.
So we have a way to dynamically allocate user inputs at contiguous location but not exactly in an array. Do we ?
Also I want to know exactly why my previous program behaving strangely.

And about C hat and C++ hat, I'll Google and see what they are.

thanks
Last edited by grvs; Jun 14th, 2008 at 11:28 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Defining arrays of variable(user specified) length

 
2
  #4
Jun 14th, 2008
>And about C hat and C++ hat, I'll Google and see what they are.
Don't waste your time doing that. What Salem was trying to convey was that people has the tendency to write C/C++ implying that it is, somehow just a programming language. When indeed C is not C++, neither C++ is C, contrary to the believe of many books and new learners. It is better to treat them as they are, two completely different programming languages with some similar syntax.

> new allocates memory in C++ and java, how its different from malloc?
Closely related to the topic. C do not have garbage collector, therefore, every time you call malloc to allocate some block of memory you must make sure that when that memory is not needed you release it by making a call to the function free().
Get to the habit of making sure that each call to malloc needs to be verified by checking that there was not problem reserving that block of memory.
e.g.
example

Not related to the topic, but some how not possible to dismiss are those call to scanf() to read an integer from user. I don't want to overwhelm you, but feeling a waste of time to learn something you are going to have to unlearn later; hence my intention to warn you now.
Don't use scanf() for reading from input. Verifying what the user enters is very hard, specially strings.
Some alternative example here.
Last edited by Aia; Jun 14th, 2008 at 11:58 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Defining arrays of variable(user specified) length

 
0
  #5
Jun 14th, 2008
More on the differences - http://david.tribble.com/text/cdiffs.htm

> Just an integer pointer.
A is the pointer.
What malloc returns is in effect the array, when you access it via A (or any other variable who's type is int*)
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 53
Reputation: grvs is an unknown quantity at this point 
Solved Threads: 0
grvs grvs is offline Offline
Junior Poster in Training

Re: Defining arrays of variable(user specified) length

 
0
  #6
Jun 14th, 2008
Originally Posted by Salem View Post
More on the differences - http://david.tribble.com/text/cdiffs.htm

> Just an integer pointer.
A is the pointer.
What malloc returns is in effect the array, when you access it via A (or any other variable who's type is int*)
Thanks doubt solved completely...

Originally Posted by Aia View Post

> new allocates memory in C++ and java, how its different from malloc?
Closely related to the topic. C do not have garbage collector, therefore, every time you call malloc to allocate some block of memory you must make sure that when that memory is not needed you release it by making a call to the function free().
Get to the habit of making sure that each call to malloc needs to be verified by checking that there was not problem reserving that block of memory.
e.g.
example
when I run the program as Mr. Salem said, I didn't free the memory, so a lot of thanks.

Originally Posted by Aia View Post
Not related to the topic, but some how not possible to dismiss are those call to scanf() to read an integer from user. I don't want to overwhelm you, but feeling a waste of time to learn something you are going to have to unlearn later; hence my intention to warn you now.
Don't use scanf() for reading from input. Verifying what the user enters is very hard, specially strings.
Some alternative example here.
yeah I got a little overwhelmed but I don't want bad habits as well... as overwhelming now seems better than frustration/embarrassment later. Thanks again.
Last edited by grvs; Jun 14th, 2008 at 1:34 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC