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

Arrays in C++

Name: Anonymous 2012-11-12 21:48

So lets say I have an array, int array[3] = {123, 456, 789}.
If I wanted to take the first number of each value of the array and store it in another array, say int first[3];, how would I do that? I tried using a pointer to read the first memory address of each value of the array but it keeps returning the full array.

Name: Anonymous 2012-11-12 22:38


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

void getFirstNumbers
(
  int * dest,
  int destLength,
  int * src,
  int srcLength
)
{
  assert(srcLength == destLength);

  //char t[10];

  for(int * pArray = src, * pFirst = dest;
      pArray != src + srcLength;
      ++pArray, ++pFirst)
  {
    /*
     *  LEAH quality
     *
     *  t = itoa(*pArray, t, 10);
     *  *pFirst = atoi(t[0]);
     *
     */

    *pFirst = *pArray;
    for( ; *pFirst > 10; *pFirst /= 10);
  }
}

int main()
{
  int array[3] = {123, 456, 789};
  int first[3];

  getFirstNumbers(first, 3, array, 3);
 
  for(int * pFirst = first;
    pFirst != first + 3;
    ++pFirst)
  {
    printf("%d\n", *pFirst);
  }

  return 0;
}

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