Is a char array more efficient than a char pointer in C?
This is really an impossible question to answer. There is no inherent efficiency of an array or a pointer. One or the other may have better or worse efficiency for a particular operation, under a particular compiler, for a particular processor architecture. But there are no absolute guarantees.
What's equally important is that one or the other may have better or worse functionality for the problem at hand. Or better or worse convenience for you, the programmer. These factors are hugely important, too, likely more important than raw efficiency.
My advice to you is that you really learn the different ways of using arrays and pointers in C, and — most importantly — really understand the concept of the "correspondence between arrays and pointers". Only after gaining that understanding, I think, will you be in a position to make any meaningful distinctions between which one might be "more efficient" for a particular problem. Realize, too, that the actual efficiency differences, if any, might be immeasurably slight.
In terms of raw allocation, it's always going to be faster to allocate an array (which mostly happens at compile time, and has almost no run-time overhead [footnote]) than to call malloc
to dynamically allocate some memory to point to. (But, much of the time, there will be an overwhelming preference towards dynamic memory allocation anyway, because of the intolerable nuisance of fixed, compile-time limits.)
In terms of copying, there will not generally be any difference whatsoever between any of the calls
memcpy(a, p, n);
memcpy(p, a, n);
memcpy(a1, a2, n);
memcpy(p1, p2, n);
for arrays a
and pointers p
. On the other hand, the best way to speed up memory copying is to not copy memory around at all, so in one sense pointers can be hugely more efficient, in that they let you do things like
p = a;
and
p1 = p2;
instead (which you obviously can't do for arrays).
Finally, there's the question of raw access. Down at the machine language level, there is going to be a significant difference between the instructions required to do
x = a[i];
versus
y = p[i];
But I can't tell you which will be faster, because it tends to vary from processor to processor and compiler to compiler.
People used to worry whether it was faster to iterate over an entire array using "array style":
for(i = 0; i < n; i++)
sum += a[i];
or "pointer style":
for(p = a; p < &a[n], p++)
sum += *p;
Once upon a time, one of these was likely to be significantly more efficient — although, again, the answer depended on the processor architecture, and tended to vary over time. Today, I believe that optimizing compilers are smart enough to choose the most efficient machine code to emit regardless of which way you write the C code.
Finally, as is always the case when asking "Which is more efficient?", the only way to find an actual answer is to code it up, for your problem statement and using your compiler and your processor, and perform careful measurements. There are so many factors that go into the efficiency question that it's usually impossible to make accurate predictions.
Footnote: I said that array allocation "mostly happens at compile time, and has almost no run-time overhead". There's one exception, which concerns large arrays which are local to functions. Those can take significant time to "allocate" if the OS has to assign a bunch more stack space at the last minute, and they can also fail if they're too big. So large, local arrays are generally disrecommended.
char *s = "Hello";
", there is none. The string literal"Hello"
is embedded into the executable, you just makes
point to it. Generally if you want to see how this stuff works you should look at the generated assembly. – Lombardochar[10] str2;
? – Seiter