Thread: difference between pointer and array

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    168

    difference between pointer and array

    my code is as follow:
    Code:
    char a[] = "ACCDDDDCCDFFF";
    char *a =  "ACCDDDDCCDFFF";
    printf("%c\n",a[1]);
    and
    printf("%c\n",*(a+1));

    which is more quickly?
    why?

    I have thought they are same speed, isn't it ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Other than syntax, there is no difference between:
    Code:
    printf("%c\n",a[1]);
    and
    Code:
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    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.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Replies: 10
    Last Post: 11-06-2005, 09:29 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM