当前位置: 动力学知识库 > 问答 > 编程问答 >

c - Pointer to a char array[10] for ptr++ arithmetic

问题描述:

My post tries to kill 2 birds with 1 stone. Sorry in advance for the ignorance.

I'm trying to create an array of strings that I can index[0] or use ptr++ to advance the array. I'm not sure if I should create an array of char pointers, or a pointer to a char array. The variables will be stored in a struct. Forgive the ignorance, I'm just having a hard time with the order of precedence of when and where to use (). I understand a basic struct, it was when I started using a pointer to a string when I started to loose syntax structure. If I can understand the syntax for this, I could apply it further to dimensional structures of arrays.

Assuming I had the assignment of the variables correct, I think I rather use ptr++ in regards to something like printf("%s", ptr++). If I understand correctly, ptr++ would move the pointer to the next string, or some for of ptr++ could. This correct? Seems like that would be faster for many, many things.

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

struct Umbrella {

char *name[10];

} Umbrella;

int main ()

{

struct Umbrella * ptr;

// Not understanding this way...

ptr->name[0] = "Some name";

// or this way. Well name++ as long as it wasn't the first string.

ptr->name++ = "Some name";

return 0;

}

Boot note: I have read "C Primer Plus" by Prata. He does well in explaining things, it is just when you start bending things to your will when you start to fall short on applying the syntax. In example, it never covered using pointers to structures to access multidimensional arrays, and it didn't cover pointer arithmetic in a manner of syntax where you would actual use it. Can anyone recommend another book that might at least braze by such approaches?

P.S. This is my second post, and I forgot to say I really like this sites text input design. Had to say it :-).

网友答案:

Well, there's char *name[10] which is really just something like :

char *name0;
char *name1;
char *name2;
// .. etc

Accessing it as ptr->name[0] will just pick the ptr->name0 as a char*.

ptr->name++ = "asdf"; is a pretty bad idea here. What it basically does is :

*(ptr->name) = "asdf";
ptr->name += 1;

Of course, you can't increase name by one here (it's an array, not a pointer) so the compiler won't allow it.


The ++ operator can be useful when iterating past objects. Example :

ptr->name[9] = nullptr; // Make sure the last element is a NULL pointer.

// Take the first element
char **it = ptr->name;

char *current;
// Loop until we reach the NULL
while ((current = *(it++)) != nullptr) {
    printf("%s\n", current);
}

The above is a (pretty ugly) way of iterating through an array.

Inserting things in a pre-allocated array:

char **it = ptr->name; // Start at the first element

*(it++) = "Hi!";
*(it++) = "This is the second message.";
*(it++) = "Hello world!";
*(it++) = nullptr; // End the array

Of course, all of this iteration stuff is from a dark past: nowadays we have C++ which takes care of most of these things for us, via std::vector, etc.

分享给朋友:
您可能感兴趣的文章:
随机阅读: