NO

Author Topic: Simple Pointer Question  (Read 3463 times)

Zooby

  • Guest
Simple Pointer Question
« on: January 18, 2017, 01:40:18 AM »
Hi,

Is it possible to return a pointer from a function?  I was attempting to do this but was having difficulties.

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Simple Pointer Question
« Reply #1 on: January 18, 2017, 06:04:12 PM »
Example:
Code: [Select]
// test_pointer.c
#include <stdio.h>
char *test(char *s)
{
return s+3;
}
int main(void)
{
char s[] = "1234567890";
puts(s);
puts(test(s));
return 0;
}
May the source be with you

Zooby

  • Guest
Re: Simple Pointer Question
« Reply #2 on: January 18, 2017, 11:09:02 PM »
Thanks!  Silly me; I must have not realized one had to set the function definition to int *.

Which brings me to a secondary question.  When is "pointer[]" format (in place of *pointer) acceptable? I was faddidling around trying new things and I just can't seem to use "s[]" like you did in the declaration after changing things.  The first example works with s*, but the second one doesn't (because it has s[]).

Code: [Select]
// test_pointer.c
#include <stdio.h>

int var = 12;
int *test(int []);


int main(void)
{
int *s = &var;
printf("%lld\n",sizeof(int));
printf("%p\n",s);
printf("%d\n",*test(s));
return 0;
}

int *test(int s[])
{
*(s+4)=5;
return s+4;
}


Code: [Select]
// test_pointer.c
#include <stdio.h>

int var = 12;
int *test(int []);


int main(void)
{
int s[] = &var;
printf("%lld\n",sizeof(int));
printf("%p\n",s);
printf("%d\n",*test(s));
return 0;
}

int *test(int s[])
{
*(s+4)=5;
return s+4;
}

(10) Error #2098: Missing '{' in initialization of 'int []'.
(10) Error #2082: Invalid initialization type; expected 'int' but found 'int *'.
(10) Error #2139: Too many initializers.

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Simple Pointer Question
« Reply #3 on: January 19, 2017, 09:37:44 AM »
at first initialize array of ints this way:
Code: [Select]
int s[] = {var};and now s, array of ints is sizeof one int and allocated from stack.

*(s+4)=5; is same as s[4]=5;
so index was out of bound in array of ints s and it is an error.
Code: [Select]
printf("%p = %d\n",&s[4],s[4]);offset is 4*sizeof(int) = 16.
« Last Edit: January 19, 2017, 11:38:57 AM by TimoVJL »
May the source be with you

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Simple Pointer Question
« Reply #4 on: January 19, 2017, 12:41:06 PM »
When is "pointer[]" format (in place of *pointer) acceptable? I was faddidling around trying new things and I just can't seem to use "s[]" like you did in the declaration after changing things.  The first example works with s*, but the second one doesn't (because it has s[]).
First of all make clear that array are not pointers and pointers are not arrays.
An array is a memory space holding a sequence of values of a specified type, a pointer is a single variable that holds the address of another variable of a specified type.
For its nature any reference to an array (in plain words the name of the array variable) in C is treated as the address of the first element of the array. So De facto the address of the first element have all properties of a pointer to a variable of the same type of the array. From this you can understand why array and pointers can share almost all the same operators, because by nature they behaves the same way.
Now let consider the subscript operator [] (yes it is a generic operator not an array specific operator). The subscript operator applies to a memory address, as a pointer or an array name, and dereferences the value at the offset specified in the square brackets. Means that give back the value retrieved from address of pointer plus the offset. The offset is always expressed in terms of type memory size not single bytes of memory. This is the so called pointers arithmetic. I.e. if we have a pointer to int
Code: [Select]
int *p; and we access to a value as
Code: [Select]
int b = p[3]; means that the address in p is added of 3 times the dimension of an int then its fetched the value at that address, it iis equivalent to
Code: [Select]
((char *)p)+3*sizeof(int).
I hope now it's more clear why array and pointers can share some operators and constructs, but they remains two different things.

Last for sake of completeness there was only one issue in the past about arrays related to the & operator (address of). Because an array is a memory reference, not an address stored, applying the operator formerly was an error. From one of the last ANSI standards (I'm not sure if C89 or C90) it is defined that the address operator applied to an array name give back again the address of the first element and is not an error. This makes definitively coincident the use of arrays names and pointers.
But if we clarified enough why syntax of them can be equivalent, they still behaves differently: if you declare an array the compiler allocates the memory space to hold all elements, if you declare a pointer the compiler allocates only the memory necessary to hold a memory address. That's why applying sizeof operator to an array give back the allocated memory for the whole array elements while applying it to a pointer give back the size in memory of a pointer.
So after declaring a pointer, and before to access data pointed by it, you must allocate memory and make the pointer point to it (i.e. using malloc). Or your program will blow up with a memory access exception...
« Last Edit: January 19, 2017, 12:50:07 PM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide