Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Arrays and pointers

Name: Anonymous 2014-02-08 8:09

void printArr(int* &arr, int size)
{
    for(int i=0;i<size;i++) cout<<arr[i]<<" ";
}

int main(int argc, char *argv[])
{
    int arr[20]={5,5,8,9,3,2,6,7,93,2,43,193,78,7,6,59,3,17,53,9};
    int* arr_p = arr;

    printArr(arr_p, 20); //this line works
    printArr(arr,20); //this line doesn't work

    pause("quit");
    return 0;

Why doesn't printArr(arr,20) work? I thought arr was just a pointer to the first value in the array.

Name: Anonymous 2014-02-23 22:30

NO EXCEPTIONS

Name: Anonymous 2014-02-23 22:41

>>2
Actually, this probably should through an exception.

Name: Anonymous 2014-02-23 23:26

arr_p is the pointer?

maybe try printArr(&arr[0], 20);

Name: Anonymous 2014-02-23 23:36

No, arr is an array. It gets evaluated as a pointer to the first element in the array, but that's an rvalue you're not going to get a non-const reference to.

Name: Anonymous 2014-02-23 23:37

I'm sure arthur could tell you... =)

Name: Anonymous 2014-02-23 23:42

void printArr(int* &arr, int size)
vS
void printArr(int arr[], int size)
?

Name: Anonymous 2014-02-24 0:51

Now, I haven't used sepples since college, by isn't int* & arr a reference to a pointer, making the array passed in printArr(arr,20); //this line doesn't work be dereferenced twice, thus try to access memory address 0x05, while the arr_p works fine?

Hopefully >>1-baka isn't still here.

Name: Anonymous 2014-02-24 1:13

Change the prototype of printArr to
printArr(int* a, into n)

A an array is implicitly a pointer but your function accepts a reference to a pointer.

Name: Anonymous 2014-02-24 1:24

>>5
>le pedophile sage

Name: Anonymous 2014-02-24 6:45

Do not use C++, it is a bad language.

Name: Anonymous 2014-02-24 7:29

Do not list to pedophiles, they are bad humans.

>>11
>le pedophile sage

Name: Anonymous 2014-02-24 16:57

Use a language with real arrays.
listArray (1,20) [5,5,8,9,3,2,6,7,93,2,43,193,78,7,6,59,3,17,53,9] :: UArray Int Int
Or if you prefer a lower bound of 0:
listArray (0,19) [5,5,8,9,3,2,6,7,93,2,43,193,78,7,6,59,3,17,53,9] :: UArray Int Int

Name: Anonymous 2014-02-24 21:30

>>1


#include <stdio.h>
#include <stdlib.h>

void printArr(int *arr, int size) {
  int i;
  for (i = 0; i < size; i++) {
    printf("%d ", arr[i]);
  }
}

int main(int argc, char *argv[]) {
    int arr[20]={5,5,8,9,3,2,6,7,93,2,43,193,78,7,6,59,3,17,53,9};
    int *arr_p = arr;

    printArr(arr_p, 20); //this line works
    printf("\n");
    printArr(arr,20); //this line doesn't work

    printf("OP IS A FAGGOT\n");

    return 0;
}

Name: Anonymous 2014-02-25 4:25

>>1
void printArr(int* &

Stopped reading there.

Name: Anonymous 2014-02-25 4:53

>>15
Me too. Those ancient languages look like
((*void)* (* (*) (*) *(& *&*)(*f)
to me.

Name: Anonymous 2014-02-25 7:12

>>15
le pedophile sage

Name: Anonymous 2014-02-25 8:26

>>17
>le pedophile >le pedophile sage

Don't change these.
Name: Email:
Entire Thread Thread List