Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

array of character pointers

Posted on 2006-07-11
7
Medium Priority
?
944 Views
Last Modified: 2012-08-14
Hey, can anyone tell me what is exactly going on when I declare an array of character pointers? how come each index of the array can point to another string?  thanks
0
Comment
Question by:yattias
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 8

Accepted Solution

by:
manish_regmi earned 100 total points
ID: 17082943
Array of pointers are just arrays containing pointers ie address.
Those address can be a location of the string stored somewhere else.

char *arr[5];

arr[5] gives you the address stored in that array.


regards
Manish Regmi

0
 
LVL 5

Assisted Solution

by:bastibartel
bastibartel earned 100 total points
ID: 17083151
Hi there,

In addition to manish_regmi, who is totally right ..

Maybe you really want to declare a pointer to char (char*  MyStr; /*or*/ char MyStr[10]; )
Then MyStr is what we like to call a 'string' but really just a pointer to a char.

The first declaration does not yet reserve any space for the string and must be allocated using
MyStr = new char[10];   // string length 9 (+1 for terminating '\0' character )

The second declaration reserves 10 char slots right away

Cheers,
Sebastian
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 100 total points
ID: 17083234
>>>> how come each index of the array can point to another string?

   // define the array of pointes on the stack
   // and initialize each pointer with NULL
   char* carr[5] = { NULL };

   // allocate (dynamic)storage for each pointer
   // note, each array element might get a different storage or keep NULL
   for (int i = 0; i < sizeof(carr)/sizeof(char*); ++i)
   {
        carr[i] = new char[100];
        // Note, the storage isn't initialized

        // you could terminate the char array by a terminating zero character
        // thus making it an empty string

        carr[i][0] = '\0';

        // or initialize any char element
        for (int j = 0; j < 100; ++j)
              carr[i][j] = '\0';

        // alternatively, you can do the same by calling memset
        memset(carr[i], 0, 100);
   }

   strcpy(carr[0], "Hello");
   strcpy(carr[1], "World");
   ...
 
Generally, you should guarantee that

- a char pointer always points to valid storage or is NULL
- the char array - where the pointer is pointing to the first element -
  is zero-terminated if contains text
- the char array is properly sized if you are writing (copying) to

An alternative to plain C char arrays are string classes, e. g. std::string, which are much easier and much more comfortable.

#include <string>
using namespace std;

      ...
      // creates 5 empty strings
      string strarr[5];
      strarr[0] = "Hello";
      strarr[1] = "World";

Regards, Alex
0
 
LVL 7

Assisted Solution

by:nafis_devlpr
nafis_devlpr earned 100 total points
ID: 17088162
When you declare a char pointer its ready to point to a single or a range of contiguouse characters (ie a character array), so when you declare an array of character pointers it actually contains an array of pointers where each of them are ready to point to a single or a range of contiguouse characters (ie a character array).
For example, if you want to declare an array of strings you can do the following:

char * arr[10]={NULL};
int i=sizeof(arr)/sizeof(arr[0]);

for (--i; i >= 0; --i)
      {
            arr[i]=new char[10];
            arr[i][0]=0;
      }

but you have to delete these allocated memory as they were created in heaps

i=sizeof(arr)/sizeof(arr[0]);

for (--i; i >= 0; --i)
      {
            delete[] arr[i];
            arr[i]=NULL;
      }

although you dont have to delete the arr array as it was craeted in stack

but there is another way in which you can create a resizable array of resizable strings using STL vector and string class

//headers
#include<vector>
#include<string>

using std::vector;
using std::string;

vector<string> str;

for(i=0; i < 10; i++)
      {
               //you can push a string object or c-style string
            str.push_back("Hello");
      }

this approach is much easier and you dont have to think about the alocation of the memory as vector and string will handle those themselves

Nafis
0
 
LVL 13

Assisted Solution

by:marchent
marchent earned 100 total points
ID: 17121905
I guess this little program should give u some idea that u asked for

#include<stdio.h>
#include<string.h>
#define MAX 100

int main()
{
      char p[MAX];
      char q[MAX];
      char *a;
      char *b;
      char *temporary;
      //assign value
      strcpy(p,"Test1");
      strcpy(q,"Test2");
      //show current value of p[] and q[]
      puts(p);      //p pass the parameter as first index of p[]
      puts(q);      //p pass the parameter as first index of p[]
      puts("\n");
      //get the address or p and q
      a = p;
      b = q;
      //show content at address a and b
      puts(a);
      puts(b);
      puts("\n");
      //swap a with b
      temporary = a;
      a = b;
      b = temporary;
      //show content at address a and b after swap
      puts(a);
      puts(b);
      puts("\n");
      return 0;
}
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

722 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