difference between pointer and array

This is a discussion on difference between pointer and array within the C Programming forums, part of the General Programming Boards category; 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 ...

  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
    21,451
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    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, 08:29 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21