Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 135,961 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,580 people online right now. Registration is fast and FREE... Join Now!




Need help with pointers and arrays

 
Reply to this topicStart new topic

Need help with pointers and arrays

Emper0r
6 Jun, 2007 - 08:22 PM
Post #1

D.I.C Head
**

Joined: 7 Dec, 2006
Posts: 59


My Contributions
Ok guys I'm trying to wrap my brain around pointers and everything to do with pointers but to be honest they confuse me. I'm trying to write a method that simply prints out all the elements in an array of ints in order. However, I can only use pointer syntax(this means no []'s) can someone please tell me if what I have below is close to being correct or if I'm way off track? Thank you very much to anyone who helps:

CODE

void printArray (int *arr, int nElts)
{
    int i;
    arr=(int*)malloc(nElts*sizeof(int));
    for(i = 0; i < nElts; i++)
       printf("%d, ", *(arr + i));
}

User is offlineProfile CardPM
+Quote Post

rameshg87
RE: Need Help With Pointers And Arrays
6 Jun, 2007 - 09:08 PM
Post #2

New D.I.C Head
Group Icon

Joined: 6 Jun, 2007
Posts: 34


Dream Kudos: 50
My Contributions
I think you are trying to print the contents of an array that already exists. If am not wrong in this, then there is no need of the malloc statement in the fuction printArray, because it allocates some new space for nElts of the array. Hence it will not print the contents of the array, because now the array pointer points to the first block of newly allocated space, not the old one. Hence if you remove that statement of malloc, it will print the contents of the array. You need to allocate memory only when you enter the elements.
User is offlineProfile CardPM
+Quote Post

Emper0r
RE: Need Help With Pointers And Arrays
6 Jun, 2007 - 09:13 PM
Post #3

D.I.C Head
**

Joined: 7 Dec, 2006
Posts: 59


My Contributions
QUOTE(rameshg87 @ 6 Jun, 2007 - 10:08 PM) *

I think you are trying to print the contents of an array that already exists. If am not wrong in this, then there is no need of the malloc statement in the fuction printArray, because it allocates some new space for nElts of the array. Hence it will not print the contents of the array, because now the array pointer points to the first block of newly allocated space, not the old one. Hence if you remove that statement of malloc, it will print the contents of the array. You need to allocate memory only when you enter the elements.


Well actually this array is created in the function. I was thinking the malloc statement creates it, sounds like I'm doing something wrong lol. What am I missing as far as the array declaration goes?
User is offlineProfile CardPM
+Quote Post

ZlxA
RE: Need Help With Pointers And Arrays
6 Jun, 2007 - 09:41 PM
Post #4

New D.I.C Head
*

Joined: 6 Nov, 2006
Posts: 10


My Contributions
QUOTE(Emper0r @ 6 Jun, 2007 - 10:13 PM) *

QUOTE(rameshg87 @ 6 Jun, 2007 - 10:08 PM) *

I think you are trying to print the contents of an array that already exists. If am not wrong in this, then there is no need of the malloc statement in the fuction printArray, because it allocates some new space for nElts of the array. Hence it will not print the contents of the array, because now the array pointer points to the first block of newly allocated space, not the old one. Hence if you remove that statement of malloc, it will print the contents of the array. You need to allocate memory only when you enter the elements.


Well actually this array is created in the function. I was thinking the malloc statement creates it, sounds like I'm doing something wrong lol. What am I missing as far as the array declaration goes?


Basically what rameshg87 said, you're taking in an array then trying to redefine the memory so either

A.) The pointer / array was empty/not allocated as you said, therefore you're printing out whatever is in the malloc'd space previously before you're populating the array or
B.) You're re-allocating the memory for the array as well as part A., printing out an empty array (or whatever was held in the memory previously before you malloc'd that memory).

Either way you're mallocing new space for an array which has nothing in it, if it was populated in another function before it was malloc'd then that memory isn't really safe and not considered yours until you malloc the pointer. I'd say try taking out the malloc statement, because it should already be allocated and populated if the only point of the function is to print out the array. I think the part you're confused about is what malloc is doing. A pointer just points to the first element in an array, but instead of creating a statically sized array, you can create a variable size array in your program using malloc. Until you malloc the memory, the memory after the first element doesn't belong to you're program and a lot of times will result in you're program core-dumping when you try to tamper with memory that doesn't belong to your program. Malloc is just reserving a set sized block of memory for you, it's not actually putting information in the array.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:27AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month