C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-09-2009, 12:14 AM   #1
Registered User
 
Join Date: Aug 2009
Posts: 2
Beginner's question concerning pointers and arrays

I am having trouble understanding why I can't get my array of strings to print using a pointer.

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;
}
When I run try to compile this code, I receive this error:

Code:
pointer-test.c: In function ‘main’:
pointer-test.c:14: error: lvalue required as increment operand
Why does the same pointer syntax not work for an int array and a string array?

Thanks for any help.
rcomj is offline   Reply With Quote
Old 08-09-2009, 12:34 AM   #2
DESTINY
 
BEN10's Avatar
 
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   Reply With Quote
Old 08-09-2009, 12:57 AM   #3
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
You can do this:
Code:
printf("%s\n", ++(*c));
but that ++ == sizeof(char), which is not what you want.

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   Reply With Quote
Old 08-10-2009, 07:55 PM   #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;
}
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)?

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   Reply With Quote
Old 08-10-2009, 11:31 PM   #5
and the hat of vanishing
 
Salem's Avatar
 
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   Reply With Quote
Old 08-11-2009, 09:09 AM   #6
DESTINY
 
BEN10's Avatar
 
Join Date: Jul 2008
Location: in front of my computer
Posts: 656
Quote:
Originally Posted by rcomj View Post
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;
}
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)?

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.
1. When you use any array as an argument to function it decomposes into pointer and thus you can do ++ with argv. Here's an example.
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
}
2. It's fine I guess.
__________________
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   Reply With Quote
Reply

Tags
pointers

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 06:32 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22