using a pointer vs array

A

arnuld

#include <stdio.h>

enum { ARRSIZE = 50 };

int main(void)
{
char* p = "This is comp.lang.c";
char arrc[] = "This is comp.lang.c";

printf("p = %s\n", p);
printf("arrc = %s\n", arrc);


return 0;
}

============= OUTPUT =============
[arnuld@dune programs]$ gcc -std=c99 -pedantic -Wall -Wextra string.c
[arnuld@dune programs]$ ./a.out
p = This is comp.lang.c
arrc = This is comp.lang.c
[arnuld@dune programs]$


May of my colleagues use char* p method, while I always use char arrc
[]. I can see both are legal but which one s preferred/recommended
way. (like using #include <conio.h> is also legal but I don't think
its recommended)
 
J

jellybean stonerfish

#include <stdio.h>

enum { ARRSIZE = 50 };

int main(void)
{
char* p = "This is comp.lang.c";
char arrc[] = "This is comp.lang.c";

printf("p = %s\n", p);
printf("arrc = %s\n", arrc);


return 0;
}

============= OUTPUT =============
[arnuld@dune programs]$ gcc -std=c99 -pedantic -Wall -Wextra string.c
[arnuld@dune programs]$ ./a.out
p = This is comp.lang.c
arrc = This is comp.lang.c
[arnuld@dune programs]$


May of my colleagues use char* p method, while I always use char arrc
[].

On my system, if I print out the address of the variables I see a big
difference.

printf("p = %ul\n", p);
printf("arrc = %ul\n", arrc);

Also if you do it more than once like the following.

int main(void)
{

char* x = "This is comp.lang.c";
char xrrc[] = "This is comp.lang.c";
char* p = "This is comp.lang.c";
char arrc[] = "This is comp.lang.c";


printf("p = %ul\n", p);
printf("arrc = %ul\n", arrc);
printf("x = %ul\n", x);
printf("xrrc = %ul\n", xrrc);

return 0;
}

You will see that the "char* p" form saves the string only once, and
gives the same pointer to "p" and "x", while the "char arrc[]" form saves
the strings twice, with the difference of the pointers being the length
of the string. One after the other. The strange thing is that if I look
at the a.out file the string "This is comp.lang.c" only occurs once.
 
N

Nick Keighley

#include <stdio.h>

enum { ARRSIZE = 50 };

int main(void)
{
  char* p = "This is comp.lang.c";
  char arrc[] = "This is comp.lang.c";

  printf("p    = %s\n", p);
  printf("arrc = %s\n", arrc);

  return 0;

}

============= OUTPUT =============
[arnuld@dune programs]$ gcc -std=c99 -pedantic -Wall -Wextra string.c
[arnuld@dune programs]$ ./a.out
p    = This is comp.lang.c
arrc = This is comp.lang.c
[arnuld@dune programs]$

May of my colleagues use char* p method, while I always use char arrc
[]. I can see both are legal but which one s preferred/recommended
way.

I don't think it matters that much. I'd use the pointer unless I
wanted
to modifiy the string.

(like using #include <conio.h> is also legal but I don't think
its recommended)

conio is non-standard. So it's neither "legal" nor "illegal".
If you need conio features then use them but be aware you've reduced
the
portability of your code.
 
B

Beej Jorgensen

arnuld said:
char* p = "This is comp.lang.c";
char arrc[] = "This is comp.lang.c";

May of my colleagues use char* p method, while I always use char arrc
[]. I can see both are legal but which one s preferred/recommended
way.

If you're never going to change the string, I prefer char*. If you are
going to modify the string, you should use char[].

http://c-faq.com/decl/strlitinit.html

I'd bet it'd likely be faster to use char* because it's not an array
that needs to be initialized at runtime. A quick horribly unscientific
test calling this function 100 million times:

char frotz(void)
{
char foo[] = "Hello there!";
return foo[2];
}

With char foo[], it took 3.0 seconds to run, whereas with char *foo,
it took 0.84 seconds. YMMV.

-Beej
 
N

nicolas.sitbon

#include <stdio.h>

enum { ARRSIZE = 50 };

int main(void)
{
  char* p = "This is comp.lang.c";
  char arrc[] = "This is comp.lang.c";

  printf("p    = %s\n", p);
  printf("arrc = %s\n", arrc);

  return 0;

}

============= OUTPUT =============
[arnuld@dune programs]$ gcc -std=c99 -pedantic -Wall -Wextra string.c
[arnuld@dune programs]$ ./a.out
p    = This is comp.lang.c
arrc = This is comp.lang.c
[arnuld@dune programs]$

May of my colleagues use char* p method, while I always use char arrc
[]. I can see both are legal but which one s preferred/recommended
way. (like using #include <conio.h> is also legal but I don't think
its recommended)

char const * p = "This is comp.lang.c";
is preferred.
 
N

nicolas.sitbon

#include <stdio.h>
enum { ARRSIZE = 50 };
int main(void)
{
  char* p = "This is comp.lang.c";
  char arrc[] = "This is comp.lang.c";
  printf("p    = %s\n", p);
  printf("arrc = %s\n", arrc);
  return 0;
}
============= OUTPUT =============
[arnuld@dune programs]$ gcc -std=c99 -pedantic -Wall -Wextra string.c
[arnuld@dune programs]$ ./a.out
p    = This is comp.lang.c
arrc = This is comp.lang.c
[arnuld@dune programs]$
May of my colleagues use char* p method, while I always use char arrc
[].

On my system, if I print out the address of the variables I see a big
difference.

  printf("p    = %ul\n", p);
  printf("arrc = %ul\n", arrc);

Also if you do it more than once like the following.

int main(void)
{

  char* x = "This is comp.lang.c";
  char xrrc[] = "This is comp.lang.c";
  char* p = "This is comp.lang.c";
  char arrc[] = "This is comp.lang.c";

  printf("p    = %ul\n", p);
  printf("arrc = %ul\n", arrc);
  printf("x    = %ul\n", x);
  printf("xrrc = %ul\n", xrrc);

  return 0;

}

You will see that the "char* p" form saves the string only once, and
gives the same pointer to "p" and "x", while the "char arrc[]" form saves
the strings twice, with the difference of the pointers being the length
of the string.  One after the other.  The strange thing is that if I look
at the a.out file the string "This is comp.lang.c" only occurs once.

printf("p = %p\n", (void*) p);
printf("arrc = %p\n", (void*) arrc);
printf("x = %p\n", (void*) x);
printf("xrrc = %p\n", (void*) xrrc);
are preferred.
It is compiler dependent if same literal string use same storage.
 
N

nicolas.sitbon

If you're never going to change the string, I prefer char*.  If you are
going to modify the string, you should use char[].
Well, you must use char[] if you want to modify it.
 
N

nicolas.sitbon

nicolas.sitbon said:
If you're never going to change the string, I prefer char*.  If you
are going to modify the string, you should use char[].
Well, you must use char[] if you want to modify it.

That's true for string literals, but string literals aren't the only
thing a char * can point at. It can point at allocated memory, for
instance. Or it can point at a pre-existing array. It can even, as a
formal parameter in a function declarator, be equivalent to char [].

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
This line unintentionally left unblank

You're right, but I was speaking about string literals as in the
example.
 
R

raashid bhatt

arnuld   said:
 char* p = "This is comp.lang.c";
 char arrc[] = "This is comp.lang.c";
May of my colleagues use char* p method, while I always use char arrc
[]. I can see both are legal but which one s preferred/recommended
way.

If you're never going to change the string, I prefer char*.  If you are
going to modify the string, you should use char[].

http://c-faq.com/decl/strlitinit.html

I'd bet it'd likely be faster to use char* because it's not an array
that needs to be initialized at runtime.  A quick horribly unscientific
test calling this function 100 million times:

    char frotz(void)
    {
        char foo[] = "Hello there!";
        return foo[2];
    }

With char foo[], it took 3.0 seconds to run, whereas with char *foo,
it took 0.84 seconds.  YMMV.

-Beej

What if i append static char foo[] = "Hello there!"; ?? then which
would u suggest to uses still char * or static one?
 
J

James Kuyper

Mark said:
By whom? For what purpose?

By many people who are aware of the fact that the behavior of a program
which attempts to modify the contents of a string literal is undefined.
For the purpose of ensuring that if they make that mistake, a diagnostic
will be mandatory.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

pointer to pointer 7
Pointer Arithmetic Problem 22
Selection-Sort in C 35
using my own malloc() 14
strtoul() behavior 39
Find the size of an array 21
Stack implementation of Linked List 25
LIFO in C 11

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top