my code is as follow:
printf("%c\n",a[1]);Code:char a[] = "ACCDDDDCCDFFF"; char *a = "ACCDDDDCCDFFF";
and
printf("%c\n",*(a+1));
which is more quickly?
why?
I have thought they are same speed, isn't it ?
my code is as follow:
printf("%c\n",a[1]);Code:char a[] = "ACCDDDDCCDFFF"; char *a = "ACCDDDDCCDFFF";
and
printf("%c\n",*(a+1));
which is more quickly?
why?
I have thought they are same speed, isn't it ?
Other than syntax, there is no difference between:
andCode:printf("%c\n",a[1]);
As for array versus pointer: if you are really so troubled, try and measure the difference. I do not expect any difference for access time, though perhaps there will be a difference in terms of when the data is loaded. But if you can actually measure a consistent significant difference for your purposes... great, pick the faster one if it is really so important.Code:printf("%c\n",*(a+1));
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Arrays and Pointers
> which is more quickly?
Well the syntax doesn't make a bean of difference. You can use either form with either arrays or pointers.
As for which one is faster, well that really depends on a host of implementation factors.
For a modern compiler on your average desktop machine, it probably doesn't make a bean of difference (none that you could reliably measure in a reasonable amount of time).
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
This is your third "which is faster" question. Well, the best answer is: stop caring about this!
Unless you are writing some extreme performance critical system, you shouldn't care. And I'm not talking about programs that are supposed to be fast. Or even games. Maybe you should care for Operating Systems. But seeing the questions you ask, you aren't up to any level close enough to care about this kind of things.
Declaring an array will allocate space for your string and allow you to change it, though. Declaring strings as a pointer type is only suitable for constants.