certifiedNoob
certifiedNoob

Reputation: 365

Initialize array of characters using pointers

This problem is driving me crazy, I'm sure I'm missing something. I need to initialize an array of chars using only pointers. Below is the code I have so far:

    int p2(){
         /* Implements problem 2 of lab */

         // Create an array 
         char **s = (char**)malloc( 11 *sizeof(char));
         char *p = *s;
         char start ='A';

         while( p != s+10){
            *p = start;
            start++;
            p++;
         }

         return(0);
    }

The problem I'm having is I don't know how to address the characters inside of the array. I understand the base address of the array is **s, and the pointer to the first element is *s. What I don't understand is how to get to **s+10 (i.e. the end of the array).

Can anyone shine some light for me??? Please!

EDIT: Ok, looks like I misunderstood the question. I appears I need to create an array of strings (thus the char ** allocation). Then I need to loop through this array, and assign each string (i.e. char *) a value 15 chars long. Please let me know if I'm understanding this correctly:

char **strings ==> strings[0 ... n ] where each element is a pointer to a char (possibly an array). There for *string ==> strings[0], *(string+1) = strings[1], etc etc.

Am I close or way off?

Upvotes: 2

Views: 2919

Answers (5)

Vyktor
Vyktor

Reputation: 20997

char **s is 2 dimensional array of characters, or array of C strings if you want. If you want to use array of characters you should use:

char *string = (char*)malloc( 11 *sizeof(char));

If you really want to initialize array of strings, at first step you're initializing array of pointers, that's:

char **s = (char**)malloc( 11 *sizeof(char *));

Please note that I'm using char * inside sizeof. Than when you may use strings, but at first you must initialize each string.

s[0] = (char*) malloc( 15*size(char)); // This is actually string, 14 characters long (NULL terminated)
char *p = s[0]; // p is pointer to beginning of your string now

And there's two way how to address your string:

s[0][3] // 4th character of your string
p[3]

Or if you want to use just pointers:

char *p = *(s+0);
*(p+3); // 4th character
*((*(s+0))+3) // To do it "hardcore"

EDIT: added an example

When you have **char p and use p++ or p + 1, C increases memory address. *p operator tells compiler that you now want to work with data stored in memory, not with pointer. Therefor those two syntax do the same:

p[10] = 'a';
*(p+10) = 'a';

So if you want traverse both your dimensions, you should use:

for( int i = 0; i < 11; i++){
    char *p = *(s+i);
    for( int j = 0; j < 10; j++){
        *(p + j) = 'a'; // Say you wanna fill them with as
    }

    // You may also use this syntax:
    while( p < (*(s+i) + 10)){ // or use != instead of <
        *p = 'a';
        p++;
    }
}

Upvotes: 4

Dan Tumaykin
Dan Tumaykin

Reputation: 1253

Why should you use a double pointer for creating an array of chars?

char *s = (char *) malloc(10 * sizeof(char));
char *start = s; // save starting pointer

for(; s < start+10; s++)
    *s = 'a';

return 0;

If you allocate char ** essentially you're allocating an array of pointers to char(eg. array of strings). If you need char array, allocate char array. If you start working with pointers a design of stack and heap can be really helpful.

Upvotes: 1

Drew Dormann
Drew Dormann

Reputation: 63704

The code is falling apart here.

char **s = (char**)malloc( 11 *sizeof(char));

You're allocating enough memory for 11 chars, which sounds like what you want to do.

However, you're casting the address to those 11 chars to a (char**) as if you were allocating space for pointers, not chars.

Upvotes: 1

sverre
sverre

Reputation: 6919

First, you don't need a char ** unless you need an array of arrays.

Second, you can get to the end of the array with (*s)[10] or *((*s)+10)

Third, in C programming, don't cast the result of malloc()

Upvotes: 1

James M
James M

Reputation: 16718

I think you meant this:

     char *s = (char*) malloc(11 *sizeof(char));
     char *p = s;

In which case you'd address the characters with s[x]

Upvotes: 2

Related Questions