Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 318 Vote(s) - 3.42 Average
  • 1
  • 2
  • 3
  • 4
  • 5
*(a++) is giving error but not *(a+1)?? where a is array name?

#1
In following code:

void main()
{
char a[]={1,5,3,4,5,6};
printf("%d\n",*(a++)); //line gives error: wrong type argument to increment
printf("%d\n",*(a+1));
}

What is the difference between line 4 and line 5. I am not getting any error or warning with line 5.



Reply

#2
`a` is an array object and not a pointer so you could not use the operation `a++`
for an array object. because this is equivalent to :

a = a+ 1;

Here you are assigning to the array object a new value which is not allowed in C.

`a + 1` return a pointer to the element 1 of your `a` array and it's allowed
Reply

#3
Okay seriously bad coding practices: However let's first address your issue:

printf("%d\n",*(a++)); //this lines gives error: wrong type argument to increment

can't be used because a is an implicit reference to the array;

You CAN do this:

char b[]={1,5,3,4,5,6};
char *a = b;
printf("%d\n",*(a++)); //this lines will not give errors any more

and off you go...

Also `*(a++)` is NOT the same as `*(a+1)` because `++` attempts to modify operand whereas `+` simply add one to constant `a` value.

Reply

#4
'a' *behaves like a* const pointer.
'a' cannot change it's value or the address it is pointing to. This is because compiler has statically allocated memory of size of array and you are changing the address that it is referencing to.

You can do it as follows instead

void main()
{
char a[]={1,5,3,4,5,6};
char *ch;
ch=a;
printf("%d\n",*(ch++)); //this lines gives error no more
printf("%d\n",*(ch+1));
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through