Gonzalez Gonzalez - 3 months ago 7
C Question

Can't increment pointer position

Why I can not increment the position of the pointer array?

typedef char SmallBuffer[20];
SmallBuffer sb2, sb1 = "wtf!";

while(*sb1 != '\0'){
.....
++sb1;
}


when compiling the gcc compiler returns :


main.c:19:38:
error: lvalue required as increment operand

++sb1
;


this happening for integer array too.

======= Update 1 =====

I tried in many other way (of-course as a pointer)

typedef char *String;
String a = "wtf!";
while(*a != NULL){
*a = TO_UPPER(*a); ++a;
} printf("%s", a);


can anyone point me what's wrong in my code?

==== Update 2 =====

This code is from Stephen Kochan - Programming in C

Page- 311

while ( *string != '\0' )
{
*string = TO_UPPER (*string);
++string;
}


can anyone explain me this code? (i've been trying this shit for last 4 hours)

Answer

Change your code to this and it will work:

typedef char SmallBuffer[20]; 
SmallBuffer sb2,  sb1 = "wtf!";
char* sbp1= &sb1[0];
while(*sbp1 !=  '\0'){
     sbp1++;
}

sb1 isn't an lvalue but sbp1 is. sb1 is an 'array' and sbp1 is a 'pointer'. Pointers aren't the same as arrays, arrays do contain a array and pointers do contain memory addresses which possibly point to a specific array. You can rise the memory address in the pointer variable but you can't rise the array.

Another possibility is to rise the index of the array like so:

typedef char SmallBuffer[20]; 
SmallBuffer sb2,  sb1 = "wtf!";
int i= 0;
while(*sb1[i] !=  '\0'){
     i++;
}

======= Update =====

For your update; I've simplified your code a little bit to explain why it doesn't work, I also added some comments:

char* a = "wtf!"; //Create pointer 'a' and assign the memory address of the first letter of the constant char array "wtf!" to it
while(*a != NULL){
    *a = TO_UPPER(*a); //Write the uppercase of *a to the constant char which is positioned at memory place 'a'
    ++a;
}

You want to write to a constant char which isn't possible because the char is constant (logically).

You can solve the problem by making the char array "wtf!" not constant like so;

char stringArray[]="wtf!";
char* a = &stringArray[0]; 
while(*a != 0){
    *a = TO_UPPER(*a); 
    ++a;
}
printf("%s", stringArray);

Or including the typedef:

typedef char *String;
char stringArray[]="wtf!";
String a = &stringArray[0]; 
while(*a != 0){
    *a = TO_UPPER(*a); 
    ++a;
}
printf("%s", stringArray);

In this code I assume you have made a function 'TO_UPPER'. I also changed the 'printf line'. printf("%s", a) wouldn't have printed anything because the pointer is rised so it points to the terminating 0 at the end of the string. Also I replaced 'NULL' by 0. I assume you want to stop at the end of the string. Strings are terminated by a 0. 'NULL' is a keyword for another purpose.

======= Update 2=====

For your update2; You didn't include the whole code. The coded snipped will work perfectly fine if you add some lines in front of it like so;

char stringArray[] = "wtf!";
char* string = &stringArray[0];
while ( *string != '\0' )
{
    *string = TO_UPPER (*string);
    ++string;
}

But I can recommend you to not use 'string' as a variable name because it's somewhat confusing for other persons like here on StackOverflow. In c it's fine but in c++ 'string' is a keyword for a string array.