0

I am new to C and I want to implement the 99 Haskell problems in C. So far I realized that sizeof in my last-function prints 4 because the pointer has a length of 4.

But how can I get the size of my list in a function?

Code

#include <stdio.h>

int last(int *list){

    int size = sizeof(*list);
    printf("%d\n", size);
    return 0;
}

int main(){

    int *a = {10, 20, 30, 40, 50, 60, 70, 80, 90};
    last(&a);
    return 0;
}
Jon
  • 11,356
  • 5
  • 40
  • 74
  • 1
    you can't. `int* list` is a pointer to int. You need to explicitely pass the size to your function. – UmNyobe Aug 24 '14 at 16:17
  • 2
    `sizeof(*list);` is `sizeof(int);` – BLUEPIXY Aug 24 '14 at 16:18
  • 1
    It is not a list. It is a pointer that points to the first element of the array. You cannot know the size of the array from the pointer, this infirmation is not there. You need to pass it separately. – n. m. could be an AI Aug 24 '14 at 16:19
  • `int *a = {10, 20, 30, 40, 50, 60, 70, 80, 90};` is also a mistake, as is `last(&a)` as written, both should generate compiler messages. Pay attention to what your compiler is telling you ; even if it says "warning" instead of "error" it may be serious enough that your program is completely broken (as in this case) – M.M Aug 24 '14 at 19:57

1 Answers1

0
#include <stdio.h>

#define last(a) a[sizeof(a)/sizeof(*a)-1]

int main(){
    int a[] = {10, 20, 30, 40, 50, 60, 70, 80, 90};
    char ca[] = { 'x', 'y', 'z' };
    printf("%d\n", last(a));
    printf("%c\n", last(ca));
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • that would print 90, not 9. – Urler Aug 24 '14 at 16:38
  • @Stochastic13 [Problem 1 Find the last element of a list.](http://www.haskell.org/haskellwiki/99_questions/1_to_10) – BLUEPIXY Aug 24 '14 at 16:44
  • I think OP was asking for the size of the list, not the last element of the list. – Urler Aug 24 '14 at 16:48
  • @Stochastic13 I think it is contained, even if so. In my opinion, `last` function should not return the length of the array. – BLUEPIXY Aug 24 '14 at 16:50
  • This is not what the OP asked for and no, the answer is not "contained" because, in the OP's case, you have a pointer, not an array. -1 – Ed S. Aug 24 '14 at 16:56
  • @EdS. Such a specification is incorrect because OP can not determine the size of the sequence of the original by passing a pointer to the function. – BLUEPIXY Aug 24 '14 at 16:59
  • also `int *a = {10, 20, 30, 40, 50, 60, 70, 80, 90};` is incorrect. – BLUEPIXY Aug 24 '14 at 17:15
  • Exactly, so the answer is "you can't" – Ed S. Aug 24 '14 at 19:01
  • @EdS. Remember that the purpose of the OP is the implementation of the `last`. – BLUEPIXY Aug 24 '14 at 19:04
  • True, but even then, not very useful in practice as it will silently accept a pointer (and fail horribly). No professional code base would be likely to have your implementation in it. How about a proper function which takes a `length` argument? That solves both problems. – Ed S. Aug 24 '14 at 19:11
  • Of course, it is possible that there be something better a little more, but it is not required, perhaps. – BLUEPIXY Aug 24 '14 at 19:13
  • To be fair, I moved the goal posts a bit with that last comment, so I will remove the -1 – Ed S. Aug 24 '14 at 19:15