What is the difference between a pointer and an array in the following context?

#include <cstring> int main() { char *pName = new char[10]; char dummy[] = "dummy"; strcpy(pName + 0,dummy);//how this is different from -->this works strcpy(pName[0],dummy);//this one...--> error C2664: 'strcpy' : //cannot convert parameter 1 //from 'char' to 'char *' } 
+4
source share
6 answers
  • pName [0] is the first element in an array of characters ( one character )
  • pName is a shortcut to & pName [0] (a pointer to the first element of your array)

The reason you get the error message is because strcpy expects a pointer to char (char *) and not to char (which is pName [0])

+12
source

When working with pointers and arrays in C or C ++, it really helps to recognize them as very different constructs (I think one of the best books explaining this difference is a book called "Deep C Secrets", if I remember correctly ) What is stopping the waters is that there is a one-way silent conversion allowed from pointers to array names (inconsistency in the processing of the language of variable names), but it is very important not to interpret the existence of this decay phenomenon as implying equivalence.

To help us talk about this, let's introduce the idea of ​​a β€œmemory cell." We model the memory cell as having two attributes:

 a) value b) address 

Then we can model a simple C ++ variable as having two attributes (we do not need types at this low level of abstraction):

 c) name d) memory cell 

Like most models, it has some drawbacks (it does not deal with an array with more than one element, but it is enough for our purposes).

So for example:

 // non-array variable: name 'i', and memory cell: value=3, address=0x0A int i = 3; // non-array variable: name 'p', and memory cell: value=0x0A, address=0x0B int *p = &i; // array variable: name 'a', and memory cell: vale=4, address=0x0C int a[1] = { 4 }; // non-array variable: name 'b', and memory cell: value=0x0C, address = 0x0D int (*b)[1] = &a; // non-array variable: name 's', and memory cell: value=0x0C, address = 0x0E int *s = &a[0]; // non-array variable: name 't', and memory cell: value=0x0C, address = 0x0F int *t = a; // Here is the key difference! read on... 

Now here is the main difference between an array variable and a non-array variable (pointer) C ++:

When a variable name is computed in C ++, it always calculates the value of its memory cell with one exception: if the variable names an array variable.
If the variable is the name of an array, it calculates the address of the memory cell.
The above two lines deserve attention.

Here are a few examples to help clarify the consequences (see the above variables):

 int k = i; // the 'i' name evaluates to the value of its cell, so 'k' is set to 3 int *q = p; // 'p' evaluates to the value of its cell, so 'q' is set to 0x0A int *r = a; // 'a' evaluates to the *address* of its cell, so 'r' is set to 0x0C int (*c)[1] = b; // 'c' is set to 0x0D 

This should in no way mean that the array variable is the same as the pointer variable.
They have different types in different ways, and any attempt to treat them as the same thing (that is, to define the variable name as an array in one translation unit and as a pointer to another) will lead to bad things.

So, for example, for. do not do that:

  // myproj_file1.cpp
 int array [100] = {0};  // here 'array' evaluates to the * address * of the first memory cell

 // myproj_file2.cpp
 extern int * array;  // here 'array' evaluates to the * value * of the first memory cell 
             // Assuming the linker links the two
             // what it does if you read the assembly, is something like this: 
             // extern int * array = (int *) array [0];
             // but it doesn't have to, it can do anything, since the behavior is undefined

Hope this helps. If you still feel that further clarification may help, please ask the following question and feel free to get a copy (library?) Of this book "Deep C Secrets" :)

- The postscript page function types and their names and their decay are not relevant to most of this post
postscript I also did not intentionally notice that conversion of an array to a pointer does not occur when arrays are bound to reference types

+3
source

There is no difference. Both of them will crash because you did not allocate space for pName. :) [EDIT: no longer asked about the accident]

The main difference is stylistic, which is often influenced by what corresponds to the written surrounding code - basically access to the array or basically access to the pointer.

(EDIT: suppose you really meant & pName [0], as Brian Bondi pointed out.)

0
source

Technically, strcpy(pName[0], dummy); wrong. Even if the memory was allocated for him.

This is because pName[0] is of type 'char', and pName + 0 is of type char *. They both refer to the same memory, but in different ways.

Then the compiler can turn strcpy(pName[0], dummy); in strcpy((char*) pName[0], dummy); that is dangerous implicit casting. If your compiler is half decent, you will get a warning or error (as you see with your "C2664 error").

0
source

An array is simply a pointer automatically (usually) assigned to an automatically allocated block of memory. Taking your example, you can declare a mannequin in the same way:

 char dummy[] = "dummy"; char *dummy = "dummy"; 

And then you can use array syntax or pointer syntax to access data:

 char ch = dummy[0]; // get the first element of the array char ch = *dummy; // get the data pointed to by dummy 

Both [] and * redirection pointers and arrays can be used, therefore the following equivalents:

 array[N]; *(ptr + N); 

Given the second form, (ptr + N) is still a pointer, further along the array. This is why it is syntactically correct in your example. ptr[N] is a pointer dereference and is char (in this context).

0
source

pName is a pointer to the newly allocated memory. char *pName = new char[10];

dummy is also an array / pointer. char dummy[] = "dummy";

pName is a pointer and points to the base address, even you add (pName + 0), still pointing to the same place in memory, because your only addition is 0. strcpy(pName + 0,dummy);

strcpy use a pointer variable and your pass value in the first argument, so you get strcpy(pName[0],dummy) error strcpy(pName[0],dummy)

0
source

All Articles