473,326 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

name of the array decays to pointer..

Expand|Select|Wrap|Line Numbers
  1. char *p="Hello";
  2. char q[]="Hello";
  3. p++;
  4. q++;// THIS LINE GIVES ERROR,LValue reqd as increment operand  
  5. printf("%s %s\n",p,q);
  6.  
The name of the array decays to pointer except 2 at 2 cases(sizeof and & operator).
So when q in the above example decays to pointer,why it is giving error in q++ whereas p++ is doing fine.?
Mar 17 '11 #1
3 2193
Banfa
9,065 Expert Mod 8TB
Because q is not actually a pointer it is an array and you can't increment an array, that operation has no meaning.

"name of the array decays to pointer" means that when you use an array name its type is a pointer type but it is still an array. So you could do char *r = q + 1;, q decays to a pointer and the + 1 operation is valid on this pointer because it does not involve trying to alter q.
Mar 17 '11 #2
weaknessforcats
9,208 Expert Mod 8TB
"name of the array decays to pointer" means that when you call a function using the array as an argument, the function sees the array as the address of element 0. That is, the function sees the array as a pointer.

There is no way the function can know tge size of the array based on a pointer or even if there is an array at all. This is the "decay of array". That's why in C a string has a \0 at the end. C functions that have char* arguments ASSUME an array is char but since they don't know the number of chars, they process until they reach the \0.

Decay of array, however, is not your problem with q.

q is the address of a stack array which is managed by the compiler. Changing q in effect changes the address of the stack array. Therefore q is not an L_VALUE. Therefore q++ is an error.

p, on the other hand is the same situation but p is your variable and not the compiler's. When you p++ you lose the address of the H in the literal. As far as the compiler is concerned, that's your business.
Mar 18 '11 #3
donbock
2,426 Expert 2GB
Let's look at it from another angle.

Line 1 does several things: it stores the null-terminated string "Hello" in an unnamed buffer; it creates pointer variable p; and it initializes p to point at the unnamed buffer.

Line 2 stores the null-terminated string "Hello" in a buffer named q.

Line 3 increments the value of pointer p, causing it to point at the second element in the unnamed buffer.

Line 4 ... what do you think this line should do?
Mar 18 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Nils | last post by:
Hi, I want to create a dynamic array with pointer, without allocation of the memory. I tried it so: objekt **ob= new objekt; It is not working, because he will parameter for the Konstructor...
4
by: herrcho | last post by:
#include <stdio.h> int main() { char *name; int i; name = "Jung Jae Une"; name = "Han Woo Ryong"; name = "Byun Ji Ha";
5
by: pandapower | last post by:
Hi, I know about the equivalence of pointer and arrays.But my doubt comes when its for multidimentional arrays.I have read the C faq but still have some doubts. Suppose I have a declaration as...
9
by: sangeetha | last post by:
Hello, Is there any performance difference in using of the following two declaration? int (*ptr); //Array of 10 int pointers int *ptr; // pointer-to-array of 10. Regards, Sangeetha.
7
by: Kathy Tran | last post by:
Hi, Could you please help me how to declare an araay of pointer in C#. In my program I declared an structure public struct SEventQ { public uint uiUserData; public uint uiEvent; public uint...
53
by: Tomás | last post by:
Some programmers treat arrays just like pointers (and some even think that they're exactly equivalent). I'm going to demonstrate the differences. Firstly, let's assume that we're working on a...
6
by: Lighter | last post by:
How to read "The lvalue-to-rvalue, array-to-pointer, and function-to- pointer standard conversionsare not applied to the left expressions"? In 5.18 Comma operator of the C++ standard, there is a...
5
by: erfan | last post by:
hi, here is my code : //learn a pointer points to functions #include<stdio.h> #include<stdlib.h> #include<math.h> double Add(double x,double y) {return (x+y);} double Sub(double x,double y)...
7
by: lovecreatesbea... | last post by:
Is it always legal to cast expressions of type of multi-dimension array to type of pointer? Including: T to T* , T to T* , T to T* , and so on... For example: int *mtxrot1d(int *p,...
10
by: Richard Heathfield | last post by:
Stephen Sprunk said: <snip> Almost. A function name *is* a pointer-to-function. You can do two things with it - copy it (assign its value to an object of function pointer type, with a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.