Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 962
  • Last Modified:

array of character pointers

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
yattias
Asked:
yattias
5 Solutions
 
manish_regmiCommented:
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
 
bastibartelCommented:
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
 
itsmeandnobodyelseCommented:
>>>> 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
 
nafis_devlprCommented:
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
 
marchentCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now