1

Im working on a project and I am stumped on this part.

I need to read words from stdin and place them in a char array and use an array of pointers to point to each word since they will be jagged. where numwords is an int read in representing the number of words.

    char words[10000];
    char *wordp[2000];

the problem is that I can only use the pointers to add the words.I can no longer use the [] to help.

    *wordp = words; //set the first pointer to the beginning of the char array. 
    while (t < numwords){
      scanf("%s", *(wordp + t))  //this is the part I dont know
      wordp = words + charcounter; //charcounter is the num of chars in the prev word
      t++;
    }

    for(int i = 0;words+i != '\n';i++){
      charcounter++;
    }

any help would be great I am so confused when it comes to pointers and arrays.

6
  • 2
    wordp = words won't even compile. Show us your real code. Commented Mar 20, 2013 at 22:29
  • I know this wont compile thats the problem i am totally lost I cant figure out how to do this Commented Mar 20, 2013 at 22:36
  • 1
    Do you have 10,000 words? Or a really long string? (You've declared the latter). The next line declares 2000 pointers. Commented Mar 20, 2013 at 22:38
  • Yea their can be up to 2000 words of varying lengths so if you have 2000 words and some are of length 4 chars plus a null char then that will be 10000 chars. Commented Mar 20, 2013 at 22:44
  • Ok, well you can have 2000 pointers that each point to individual elements of a 10,000 long array, yes. You just need to keep track of where you are so you don't overwrite things. Commented Mar 20, 2013 at 22:49

2 Answers 2

1

Your code will be much more manageable if you use an additional pointer reference and increment that directly. In this way you won't have to do any mental math. Additionally you need to be incrementing the reference before reading in the next string, scanf doesn't move the pointer for you.

char buffer[10000];
char* words[200];

int number_of_words = 200;
int current_words_index = 0;

// This is what we are going to use to write to the buffer
char* current_buffer_prt = buffer;

// quick memset (as I don't remember if c does this for us)
for (int i = 0; i < 10000; i++)
    buffer[i] = '\0';

while (current_words_index < number_of_words) {

    // Store a pointer to the current word before doing anything to it
    words[current_word_index] = current_buffer_ptr;

    // Read the word into the buffer
    scanf("%s", current_buffer_ptr);

    // NOTE: The above line could also be written
    // scanf("%s", words[current_word_index]);

    // this is how we move the buffer to it's next empty position.
    while (current_buffer_ptr != '\n') 
        current_buffer_ptr++;

    // this ensures we don't overwrite the previous \n char
    current_buffer_ptr++;

    current_words_index += 1;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this will help me so much!
1

What you want to do is relatively straightforward. You've got an array of 10,000 chars for storage, and 2000 pointers. So to start with you'll want to assign the first pointer to the start of the array:

wordp[0] = &words[0];

In pointer form this is:

*(wordp + 0) = words + 0;

I've used the zeros to show how it relates to the arrays. In general, to set each pointer to each element:

*(wordp + i) == wordp[i]
words + i    == &words[i]

So all you need to do is keep track of where you are in the pointer array, and as long as you've assigned correctly, the pointer array will keep track of the position in your char array.

2 Comments

Oh gotcha thanks so much pointers are just hard for me to wrap my head around
@D_Man - they can be difficult. Just remember that a pointer holds an address, and that any element of an array has an address in memory, so can be pointed to. I also fixed a (stupid) typo in the stuff above.

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.