473,394 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

help with arrays and pointers

I'm still a student, and I'm really having a hard time understanding how arrays of pointers and pointer arrays work. (dealing with C++)


I'm trying to make a 2D Array with pointers, where the first array is a list of X pointers (decided by the user at runtime, and each pointer points to a different array of Y characters, entered by the user

Then I need to manipulate the array of pointers so that when displayed, the character Arrays are displayed in alphabetic order. (AKA "Andrea", "Billy", "Bob", "Tera", "Treven", "Willy", etc.)


I'm less worried about the putting in alphabetic order, and more having trouble figuring out how to let the 2D array to be manipulated, in that each character entered is only one element in the array, which can be displayed consecutively like a string while stopping at the end of string mark, individually checked (so as to allow sorting for each letter), and how to use the pointers in the first array to call the second array and alter it.
...
Maybe I just need an easy-to-understand explaination of how pointers, pointer arrays, and arrays of pointers work, and how they are used, cus I'm so confused that I don't even know exactly what I need help with..

here's some snippets of the code I'm trying to work out, if it helps describe my problem.

the Array:
Expand|Select|Wrap|Line Numbers
  1. Array = new char * [NumEntries]; //array of row pointers
  2.     for (int i = 0; i < NumEntries; i++)
  3.         Array [i] = new char [Length+1];
for taking input:
Expand|Select|Wrap|Line Numbers
  1. int TakeInput (char **Array, int NumEntries, int Length)
  2.     {
  3.     char Input;
  4.     int CurrCount (0);
  5.  
  6.  
  7.     for (int i = 0; i < NumEntries; i++)
  8.         {
  9.         cout << "Enter Name: " << i+1;
  10.  
  11.         while ((Input = cin.get ()) != '\n')
  12.             {
  13.             Array [i][CurrCount] = Input;
  14.             CurrCount++;
  15.  
  16.             if (CurrCount >= Length)
  17.                 break;
  18.             else;
  19.             }
  20.             Array [i][CurrCount+1] = '\0';
  21.             cout << endl;
  22.         }
  23.     return **Array;
  24.     }
for displaying them:
Expand|Select|Wrap|Line Numbers
  1. void Display (char **Array, int NumEntries)
  2.     {
  3.     for (int i = 0; i < NumEntries; i++)
  4.     {
  5.         for (char j = 0; j != '\n'; j++) 
  6.         cout << Array [i][j];
  7.     }
  8.     }
  9.  
for sorting them:
Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i <= EndCount; i++)
  2.             for (int j = 0; j <= Length; j++)
  3.                 {
  4.                 if ((Array [i][j]) > (Array [i + 1][j]))
  5.                     {
  6.                     Temp    = Array [i];
  7.                     Array [i]    = Array [i+1];
  8.                     Array [i+1] = Temp;
  9.                     Swapped = true;
  10.                     }
  11.                 else;
Thanks for your time, I'd appreciate any help you could give me.
Apr 8 '10 #1
1 1409
donbock
2,426 Expert 2GB
Take a look at Arrays Revealed.
Apr 8 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: CoolPint | last post by:
I would be grateful if someone could point out if I am understanding correctly and suggest ways to improve. Sorry for the long message and I hope you will kindly bear with it. I have to make it...
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
13
by: Ben | last post by:
I have a program which is using a lot of memory. At the moment I store a lot of pointers to objects in std::vector. (millions of them) I have three questions: 1) Lets say the average Vector...
1
by: Geoff | last post by:
I was wondering if anyone could help me with a problem I am having. I am trying to read in a list of numbers from a file and then sort them using pointers and malloc() and free(). I know how to...
79
by: Me | last post by:
Just a question/observation out of frustration. I read in depth the book by Peter Van Der Linden entitled "Expert C Programming" (Deep C Secrets). In particular the chapters entitled: 4: The...
36
by: raphfrk | last post by:
I have the following code: char buf; printf("%lp\n", buf); printf("%lp\n", &buf); printf("%lp\n", buf); printf("%lp\n", buf); printf("%d\n", buf-buf);
1
by: Nathan Gilbert | last post by:
I have a function that is returning a 2D array (declared using double pointers) and I want to be able to append a row of data to the beginning and end of this array without having to create a new...
23
by: vinod.bhavnani | last post by:
Hello all, I need desperate help Here is the problem: My problem today is with multidimensional arrays. Lets say i have an array A this is a 4 dimensional static array.
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.