Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reposition array base pointer

Status
Not open for further replies.

bitwise

Programmer
Mar 15, 2001
269
US
If I have an array:

char array[] = "hello";

Now 'array' is the address to the first element. You can access elements in the array by going:

&array[0]+i

where "i" is some index value.

My question is, lets say I want to make the array "hello" become "lo" for example. How do I change the address of where 'array' currently points? I've tried several things. I would guess it would be something like this but this does not work:

array = &array[0]+i

Any thoughts?
-bitwise
 
bitwise:

I don't want to elaborate on the obvious, but you could declare a character pointer, point it to array, bump the pointer by 3, and that give you a string. You're not really changing the address of array - just point to the address 3 charaters from the beginning.

Regards,

Ed


include <stdio.h>

main()
{
char array[] = &quot;hello&quot;;
char *ptr;

ptr=array;
ptr=(ptr+3);

printf(&quot;new string %s\n&quot;, ptr);
}
 
bitwise:

It's probably more portable to do this:

#include <stdio.h>

main()
{
char array[] = &quot;hello&quot;;
char *ptr;

/*ptr=(ptr+3); */
ptr=array + (3*(sizeof(char)));

printf(&quot;new string %s\n&quot;, ptr);
}
 
Thanks for your comments guys, however, that was merely an example. I really do want to change where the base of the array points. At least that is what I thought would be the best solution for writing a trim function. I figured you would increment the base pointer past all beginning spaces then assign the last space in the string the null termination value '\0'. Does that make logical sense to you guys?

PS: Even if there is a better solution for a trim function. How in the heck do you change the address of the array base pointer? It doesn't appear to be behaving like a regular pointer, and now I'm curious.

Thanks,
-bitwise
 
bitwise:

I'm not an expert in &quot;C&quot; compilers, but I don' think it's possible to change the address of array once it's assigned at runtime. array is a memory location, and not a pointer.

My opinion is, if you don't want to use another defined character pointer, you'll have to manipulate the contents of array and not the address.

Regards,

Ed
 
Ok, if that is how it is then I'll just except that. I just wanted to make sure that you couldn't do it. I seems like you should because the name of the array is just a pointer to the first element in the array so it makes sense that you could just make it point somewhere else. In anycase, if it is prohibited then I'll just have to implement it in a different way.

Thanks so much!
-bitwise
 
That's not strictly true. An array doesn't become a pointer to its first element when it is the operand of the sizeof operator or the operand of the address operator. This is why (where a is an array and p is a pointer to the same type) sizeof(a)!=sizeof(p) and &a becomes a pointer to an array and &p becomes a pointer to a pointer.

Of course, you also can't do pointer arithmetic on arrays.
Russ
bobbitts@hotmail.com
 
It's already sort of been covered, but I just wanted to re-iterate the point... the name of an array is defined as the address of the first element of the array (so in the case of int a[10]; a is the same as &a[0], i.e. effectively, it is a pointer.

However, the name of an array is a special kind of pointer, it is a pointer constant!! This means its value cannot be changed. The name of an array is considerd to be an rvalue (an rvalue can only appear on the right hand side of an assignment statement). It's value may only be referenced and not assigned.
 
Funny about int a[10]; that
a,&a[0] and &a will have the same value if you pass it as function argument.
rbobbit right, 'a' is not a simple pointer. For example &a is not a double pointer, it's still single. Compiler will ignore '&' for 'a'.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top