Hey,

I'm trying to figure out how pointers and C String work together I know the basics about pointer arithmetic but I am having trouble implementing it in my program. I am having a problem at the for loop.

/* Trying to print a string from an offst using pointer arithmatic */
#include<stdio.h>

int main(){
	char words[100] = ("This is my hundredth attept st this extremely annoying task :)");
	puts(words);
	
	/* printing one char is no problem with printf */
	printf("%c\n", *(words + 3));
	
	/* printing with putchar is also no problem so far */
	putchar(*(words + 3));
	
	/* printing with an offset of 11 and ending 21 to print the word "hundreth" */
	for(*(words + 11); !*(words + 21); *(words++)){
		putchar(*words);
	}
}

This is the error I get with the GCC compiler.

pointer3.c: In function ‘main’:
pointer3.c:15: error: lvalue required as increment operand

Thanks

Managed to figure it out, all I needed was to create an index.

/* Trying to print a string from an offst using pointer arithmatic */
#include<stdio.h>

int main(){
	char words[100] = ("This is my hundredth attept st this extremely annoying task :)");
	char *index = NULL;
	index = words + 11;
	
	puts(words);
	
	/* printing one char is no problem with printf */
	printf("%c\n", *(words + 3));
	
	/* printing with putchar is also no problem so far */
	putchar(*(words + 3));
	putchar('\n');
	puts("************************************");	
	putchar(*index);
	putchar('\n');
	
	/* printing with an offset of 11 and ending 21 to print the word "hundreth" *
	for(*(words + 11); index != *(words + 21); index++){
		putchar(*(words + index));
	}*/
	
	for(*index; *index != *(words + 21); *index++)
		putchar(*index);
		
	putchar('\n');
}
/*USING GRAPHICS IN C
THIS IS A COLOUR GAME INSTRUCTIONS
1-Chose any two digit number,add togather both digits,and then subtract
  the total from original number.
2-When you have final number look it up on the chart and find the 
  relevant color.Concentrate the color and when you have
  it clearly in your mind and click button SHOW .
3-EXAMPLE 
 73 - ( 7 + 3 ) = 63
      VINAY KUMAR
      E-mail ID :
 vinay_verma107@yahoo.co.in
*/
#include<graphics.h>
#include<dos.h>
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>

//==============================
int X1,X2,Y1,Y2,button,s1,a,b,x,y,bt;

void show();
void mainbutton(int,int,int,int,char *);
char texts[100][50]={
"RETRY","SHOW","EXIT"};
void number();
 void button1();
void mainbutton(int,int,int,int,char *);
void disp();
void mainscr();
union REGS i,o;
void normalbutton(int,int,int,int);
int s=0,bt,mx,my,c,z ;
void main()
{
int gd=DETECT,gm,maxx,maxy;
initgraph(&gd,&gm,"\\tc\\bgi");
if(initmouse()==0)
 {
  closegraph();
  restorecrtmode();
  printf(" Mouse driver not loded");
  exit(1);
}

cleardevice();
movemouseptr(&x,&y);
mainscr() ;
disp();
getch();
}
 void mainscr()
{

int x1,x2,y1,y2,i,j ;
randomize();
c=random (100);
z=c;
	 setbkcolor(0);
	 setlinestyle(0,0,1);
	 setcolor(15);
	rectangle(0,0,getmaxx(),getmaxy());
	rectangle(1,1,getmaxx()-1,getmaxy()-1);
	rectangle(2,2,getmaxx()-2,getmaxy()-2);
	setfillstyle(SOLID_FILL,BLACK);
	bar(3,3,getmaxx()-3,getmaxy()-3);
	//==================================
	setbkcolor(0);
	setlinestyle(0,0,1);
	setcolor(WHITE);
	rectangle(40,320,600,450);
	rectangle(42,322,598,448);
	setfillstyle(SOLID_FILL,LIGHTGRAY);
	bar(44,324,596,446);
	setcolor(BLACK);
	outtextxy(248,328,"INSTRUCTIONS !");
	outtextxy(48,338,"Chose any two digit number,add togather both digits,and then subtract ");
	outtextxy(48,348,"the total from original number.");
	outtextxy(48,368,"When you have final number look it up on the chart and find the ");
	outtextxy(48,378,"relevant color.Concentrate the color and when you have ");
	outtextxy(48,388,"it clearly in your mind and click button SHOW . ");
	outtextxy(48,408,"EXAMPLE  73 - ( 7 + 3 ) = 63 ");
	outtextxy(280,418,"VINAY KUMAR ");
	outtextxy(280,428,"E-mail ID : vinay_verma107@yahoo.co.in");
	//showmouseptr();

y1=10;
y2=28;
for(j=0;j<5;j++)
 {
  x1=45;
  x2=63;
  y1+=29;
  y2+=29;
for(i=0;i<20;i++)
  {
   normalbutton(x1,x2,y1,y2);
   s++;
   x1+=28;
   x2+=28;
   }
}
number();
}
 void normalbutton(int x1,int x2,int y1,int y2)
 {
static int k=0,p=0;
	if(k==p)
	{
	      z=c;
	      setcolor(15);
	      rectangle(x1-3,y1-3,x2+1,y2+1);
	      rectangle(x1-3,y1-3,x2+1,y2+1);
	      rectangle(x1-4,y1-4,x2+1,y2+1);
	      rectangle(x1-4,y1-5,x2+1,y2+1);
	      setcolor(8);
	      rectangle(x1-4,y1-5,x2+2,y2+2);
	      rectangle(x1-4,y1-5,x2+3,y2+3);
	      rectangle(x1-4,y1-5,x2+4,y2+4);
	      rectangle(x1-4,y1-5,x2+5,y2+5);
	      setfillstyle(SOLID_FILL,z);
	      bar(x1,y1,x2-1,y2-1);
	      p=p+9;
	}
	else
	{
	setcolor(15);
	rectangle(x1-3,y1-3,x2+1,y2+1);
	rectangle(x1-3,y1-3,x2+1,y2+1);
	rectangle(x1-4,y1-4,x2+1,y2+1);
	rectangle(x1-4,y1-5,x2+1,y2+1);
	setcolor(8);
	rectangle(x1-4,y1-5,x2+2,y2+2);
	rectangle(x1-4,y1-5,x2+3,y2+3);
	rectangle(x1-4,y1-5,x2+4,y2+4);
	rectangle(x1-4,y1-5,x2+5,y2+5);
	//setfillstyle(SOLID_FILL,random(z));
	setfillstyle(SOLID_FILL,z+1);
	bar(x1,y1,x2-1,y2-1);
	}
	z++;
	k++;

}
void number()
{

 setcolor(z); outtextxy(46,43,"99");
 setcolor(4); outtextxy(74,43,"98");
 setcolor(3); outtextxy(102,43,"97");
 setcolor(4); outtextxy(130,43,"96");
 setcolor(4); outtextxy(158,43,"95");
 setcolor(5); outtextxy(186,43,"94");
 setcolor(7); outtextxy(214,43,"93");
 setcolor(8); outtextxy(242,43,"92");
 setcolor(5); outtextxy(270,43,"91");
 setcolor(10);outtextxy(298,43,"90");
 setcolor(26);outtextxy(326,43,"89");
 setcolor(16);outtextxy(354,43,"88");
 setcolor(12); outtextxy(382,43,"87");
 setcolor(5); outtextxy(410,43,"86");
 setcolor(6); outtextxy(438,43,"85");
 setcolor(8); outtextxy(466,43,"84");
 setcolor(z); outtextxy(494,43,"83");
 setcolor(4); outtextxy(522,43,"82");
 setcolor(3); outtextxy(550,43,"81");
 setcolor(4); outtextxy(578,43,"80");
 //========second line======
  setcolor(z); outtextxy(46,73,"79");
 setcolor(4);  outtextxy(74,73,"78");
 setcolor(3); outtextxy(102,73,"77");
 setcolor(4); outtextxy(130,73,"76");
 setcolor(4); outtextxy(158,73,"75");
 setcolor(5); outtextxy(186,73,"74");
 setcolor(7); outtextxy(214,73,"73");
 setcolor(8); outtextxy(242,73,"72");
 setcolor(5); outtextxy(270,73,"71");
 setcolor(10);outtextxy(298,73,"70");
 setcolor(26);outtextxy(326,73,"69");
 setcolor(16);outtextxy(354,73,"68");
 setcolor(12);outtextxy(382,73,"67");
 setcolor(5); outtextxy(410,73,"66");
 setcolor(6); outtextxy(438,73,"65");
 setcolor(8); outtextxy(466,73,"64");
 setcolor(z); outtextxy(494,73,"63");
 setcolor(4); outtextxy(522,73,"62");
 setcolor(3); outtextxy(550,73,"61");
 setcolor(4); outtextxy(578,73,"60");
 //=================================
 setcolor(z);  outtextxy(46,103,"59");
 setcolor(4);  outtextxy(74,103,"58");
 setcolor(3); outtextxy(102,103,"57");
 setcolor(4); outtextxy(130,103,"56");
 setcolor(4); outtextxy(158,103,"55");
 setcolor(5); outtextxy(186,103,"54");
 setcolor(7); outtextxy(214,103,"53");
 setcolor(8); outtextxy(242,103,"52");
 setcolor(5); outtextxy(270,103,"51");
 setcolor(10);outtextxy(298,103,"50");
 setcolor(26);outtextxy(326,103,"49");
 setcolor(16);outtextxy(354,103,"48");
 setcolor(12);outtextxy(382,103,"47");
 setcolor(5); outtextxy(410,103,"46");
 setcolor(6); outtextxy(438,103,"45");
 setcolor(8); outtextxy(466,103,"44");
 setcolor(z); outtextxy(494,103,"43");
 setcolor(4); outtextxy(522,103,"42");
 setcolor(3); outtextxy(550,103,"41");
 setcolor(4); outtextxy(578,103,"40");
 //==================================
 setcolor(z);  outtextxy(46,133,"39");
 setcolor(4);  outtextxy(74,133,"38");
 setcolor(3); outtextxy(102,133,"37");
 setcolor(4); outtextxy(130,133,"36");
 setcolor(4); outtextxy(158,133,"35");
 setcolor(5); outtextxy(186,133,"34");
 setcolor(7); outtextxy(214,133,"33");
 setcolor(8); outtextxy(242,133,"32");
 setcolor(5); outtextxy(270,133,"31");
 setcolor(10);outtextxy(298,133,"30");
 setcolor(26);outtextxy(326,133,"29");
 setcolor(16);outtextxy(354,133,"28");
 setcolor(12);outtextxy(382,133,"27");
 setcolor(5); outtextxy(410,133,"26");
 setcolor(6); outtextxy(438,133,"25");
 setcolor(8); outtextxy(466,133,"24");
 setcolor(z); outtextxy(494,133,"23");
 setcolor(4); outtextxy(522,133,"22");
 setcolor(3); outtextxy(550,133,"21");
 setcolor(15); outtextxy(577,133,"20");
 //================================
 setcolor(z);  outtextxy(46,163,"19");
 setcolor(4);  outtextxy(74,163,"18");
 setcolor(3); outtextxy(102,163,"17");
 setcolor(4); outtextxy(130,163,"16");
 setcolor(4); outtextxy(158,163,"15");
 setcolor(5); outtextxy(186,163,"14");
 setcolor(7); outtextxy(214,163,"13");
 setcolor(8); outtextxy(242,163,"12");
 setcolor(5); outtextxy(270,163,"11");
 setcolor(10);outtextxy(298,163,"10");
 setcolor(26);outtextxy(327,163,"9");
 setcolor(16);outtextxy(355,163,"8");
 setcolor(12);outtextxy(383,163,"7");
 setcolor(5); outtextxy(411,163,"6");
 setcolor(6); outtextxy(439,163,"5");
 setcolor(8); outtextxy(467,163,"4");
 setcolor(4); outtextxy(495,163,"3");
 setcolor(4); outtextxy(523,163,"2");
 setcolor(3); outtextxy(551,163,"1");
 setcolor(15); outtextxy(579,163,"0");
  }
 void button1()
 {
  //==================button=========
 setcolor(15); outtextxy(230,260,"RETRY");
 setcolor(15); outtextxy(290,260,"SHOW");
 setcolor(15); outtextxy(350,260,"EXIT");
 }

//================================
void disp()
{

Y1=250;
Y2=275;
for(b=0;b<1;b++)
 {
  X1=225;
  X2=275;
for(a=0;a<3;a++)
  {
   mainbutton(X1,X2,Y1,Y2,texts[s1]);
   s1++;
   X1+=60;
   X2+=60;
   }
}

while(1)
{
getmousepos(&button,&x,&y);
Y1=250;
Y2=275;
for(b=0;b<1;b++)
 {
  X1=225;
  X2=275;
  for(a=0;a<3;a++)
  {
	if((x<X2&&x>X1)&&(y<Y2&&y>Y1))
	{
	if((button&1)==1)
	 {
	 bt=b*1+a;
	setcolor(15);
	outtextxy(X1+5,Y1+10,texts[b*1+a]);
	if(bt>4)
	exit(0);
	delay(10);
	delay(250);
	delay(10);
	switch (bt)
	{
	case  0 :
		//cleardevice();
		clearviewport();
		number();
		mainscr();
		disp();
		break;
	case  1 :
		show();
		break;

	case  2 :
		hidemouseptr();
		cleardevice();

		exit(0);
		break;
	       }
	    }
	}
	X1+=60;     //NEXT COLUMN BUTTON
	X2+=60;
	button1();
}
} showmouseptr();
 }

}
void mainbutton(int X1,int X2,int Y1,int Y2,char *texts)
{
setcolor(LIGHTGRAY);
rectangle(X1-2,Y1-2,X2+1,Y2+1);
rectangle(X1-3,Y1-3,X2+1,Y2+1);
rectangle(X1-4,Y1-4,X2+1,Y2+1);
rectangle(X1-4,Y1-5,X2+1,Y2+1);
setcolor(8);
rectangle(X1-4,Y1-5,X2+2,Y2+2);
rectangle(X1-4,Y1-5,X2+3,Y2+3);
rectangle(X1-4,Y1-5,X2+4,Y2+4);
rectangle(X1-4,Y1-5,X2+5,Y2+5);
//setfillstyle(SOLID_FILL,6);
setfillstyle(10,6);
bar(X1,Y1,X2-1,Y2-1);
setcolor(LIGHTGRAY);
outtextxy(X1+5,Y1+10,texts);
}
void show()
{
static int col=0,co=0;
extern int c;
extern int z;
if(col==co)
{
rectangle(50,220,130,300);
rectangle(51,221,129,299);
setcolor(6);
rectangle(52,222,128,298);
rectangle(53,223,127,297);
setfillstyle(SOLID_FILL,c);
bar(54,224,126,296);
co=co+9;
}
else
{
rectangle(50,220,130,300);
rectangle(51,221,129,299);
setcolor(6);
rectangle(52,222,128,298);
rectangle(53,223,127,297);
setfillstyle(SOLID_FILL,z);
//setfillstyle(10,6);
bar(54,224,126,296);
}
col++;
}
//==============================
initmouse()
{
 i.x.ax=0;
 int86 (0x33,&i,&o);
 return(o.x.ax);
}
hidemouseptr()
{
	 i.x.ax=2;
	 int86(0x33,&i,&o);
	 return 0;
}

showmouseptr()
{
 i.x.ax=1;
 int86(0x33,&i,&o);
 return 0;
}
getmousepos(int *button,int *x,int *y)
{
 i.x.ax=3;
 int86(0x33,&i,&o);
 *button=o.x.bx;
 *x=o.x.cx;
 *y=o.x.dx;
  return 0;
}
/* Move mouse ptr to x,y */
movemouseptr(int *x,int *y)
{
 i.x.ax=4;
 int86(0x33,&i,&o);
 o.x.cx=*x;
 o.x.dx=*y;
 return 0;
}
void graphics_cursor(int a)
{
	i.h.al=12;
	i.x.bx=mx;
	i.x.cx=my;
	i.x.dx=a;
	int86(0x33,&i,&o);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.