2

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?

5 Answers 5

4

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++;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

So how would you traverse the array (**s?). Would it be pCurr = *s; while(pCurr != *(s+10)) {... pCurr++}
@certifiedNoob I've added traversing examples, hope they are helpful
2

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]

Comments

1

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()

1 Comment

...Thanks for the 3rd line in your post. Sadly, every other answer casts return of malloc
1

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.

2 Comments

Yes, I need to allocate space for 10 pointers to characters ( a character array, aka string). Then I need to loop through the array and assign values to though pointers. How would I address the pointers I've created in **s?
@certifiedNoob: A character array/string doesn't need 10 pointers to it. One will do the job. And that's what malloc is returning. The one pointer to the beginning of the array.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.