943,908 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1182
  • C RSS
Jun 14th, 2008
1

Defining arrays of variable(user specified) length

Expand Post »
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
Reputation Points: 27
Solved Threads: 0
Junior Poster in Training
grvs is offline Offline
70 posts
since Jun 2008
Jun 14th, 2008
1

Re: Defining arrays of variable(user specified) length

> 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]
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jun 14th, 2008
0

Re: Defining arrays of variable(user specified) length

>
  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.
Reputation Points: 27
Solved Threads: 0
Junior Poster in Training
grvs is offline Offline
70 posts
since Jun 2008
Jun 14th, 2008
2

Re: Defining arrays of variable(user specified) length

>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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Jun 14th, 2008
0

Re: Defining arrays of variable(user specified) length

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*)
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jun 14th, 2008
0

Re: Defining arrays of variable(user specified) length

Click to Expand / Collapse  Quote originally posted by Salem ...
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...

Click to Expand / Collapse  Quote originally posted by Aia ...

> 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.

Click to Expand / Collapse  Quote originally posted by Aia ...
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.
Reputation Points: 27
Solved Threads: 0
Junior Poster in Training
grvs is offline Offline
70 posts
since Jun 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Calling C++ library functions in C code
Next Thread in C Forum Timeline: how to free pointers which are to be returned





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC