![]() |
| | #1 |
| Registered User Join Date: Aug 2009
Posts: 2
| Beginner's question concerning pointers and arrays Here is my code so far: Code: #include <stdio.h>
int main() {
int a[] = {1, 10, 100, 1000};
int *b = a;
char *c[] = {"hello", "how", "are", "you?"};
printf("%d\n", *b);
printf("%d\n", *++b);
printf("%d\n", *++b);
printf("%s\n", *c);
printf("%s\n", *++c);
return 0;
}
Code: pointer-test.c: In function ‘main’: pointer-test.c:14: error: lvalue required as increment operand Thanks for any help. |
| rcomj is offline | |
| | #2 |
| DESTINY Join Date: Jul 2008 Location: in front of my computer
Posts: 656
| c is an array of char pointers, which means c is actually an array, and that's why you can't use ++ operator with it. Whereas b is a poniter(not an array), that's why ++b is legal. Take this as an example. Code: int a[]={1,2,3,4};
printf("%p",(void *)++a); // again the same error coz a is an array
__________________ HOPE YOU UNDERSTAND....... for( ; ; ) printf("If you can't make it good, at least make it look good"); PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D. IDE- Microsoft Visual Studio 2008 Express Edition |
| BEN10 is offline | |
| | #3 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| You can do this: Code: printf("%s\n", ++(*c));
Consider subscript notation: Code: printf("%s\n", c[1]);
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is offline | |
| | #4 |
| Registered User Join Date: Aug 2009
Posts: 2
| Thanks for the help. I appreciate both of your replies. I have two further questions about the following code: Code: #include <stdio.h>
int main(int argc, char *argv[]) {
int a[] = {1, 10, 100, 1000};
int *b = a;
char *c[] = {"hello", "how", "are", "you?"};
char **d = c;
printf("%d\n", *b);
printf("%d\n", *++b);
printf("%d\n", *++b);
printf("%d\n", *++b);
printf("%s\n", *d);
printf("%s\n", *++d);
printf("%s\n", *++d);
printf("%s\n", *++d);
while (--argc > 0)
printf("%s\n", *++argv);
return 0;
}
2) Is my way of using char **d sloppy at all? After reading BEN10's answer, I had the idea of using a pointer to a pointer to a char, but I don't know if that is a convoluted way of doing things. My goal with this little program is to understand pointers better and not use indexing, since I already understand how indexing works. Thanks again for any help. |
| rcomj is offline | |
| | #5 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| > 1) Why is it that I can manipulate argv, which is an array of char pointers, using ++ but couldn't do the same to c (in the original post)? Because it's a pointer. char *argv[] could equally be written as char **argv Using [] in the context of a function parameter doesn't add any "true array" properties to it.
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #6 | |
| DESTINY Join Date: Jul 2008 Location: in front of my computer
Posts: 656
| Quote:
Code: #include<stdio.h>
void f(int []);
int main(void)
{
int a[]={1,2,3};
printf("%p",(void *)a);
f(a);
getch();
}
void f(int b[])
{
printf("\n%p",(void *)++b);//++ is legal here
}
__________________ HOPE YOU UNDERSTAND....... for( ; ; ) printf("If you can't make it good, at least make it look good"); PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D. IDE- Microsoft Visual Studio 2008 Express Edition Last edited by BEN10; 08-11-2009 at 09:14 AM. | |
| BEN10 is offline | |
![]() |
| Tags |
| pointers |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| pointers & arrays and realloc! | zesty | C Programming | 14 | 01-19-2008 04:24 PM |
| a question on pointers, structs and arrays | onefootswill | C Programming | 3 | 12-06-2007 01:27 AM |
| Pointers and multi dimensional arrays | andrea72 | C++ Programming | 5 | 01-23-2007 04:49 PM |
| pointers | InvariantLoop | C Programming | 13 | 02-04-2005 09:32 AM |
| Help understanding arrays and pointers | James00 | C Programming | 2 | 05-27-2003 01:41 AM |