0

I wrote such code, but it shows only this:

2
2
2
2
2
2 

...

and thats not the output I would like to get.

Heres the code:

void fun(int *tab, int n)
{
    int i, wsk = &tab;
    for(i=0; i<n; i++)
    {
        printf("%d\n", *(tab+1));
    }
}

int main(int argc, char **argv)
{
    int tab[] = {1,2,3,4,5,6};
    fun(tab, 6);

    return 0;
}

The second version I tried, doesnt work at all:

void fun(int *tab, int n)
{
    int i, wsk = &tab;
    for(i=0; i<n; i++)
    {
        printf("%d\n", *(wsk+i));
    }
}

int main(int argc, char **argv)
{
    int tab[] = {1,2,3,4,5,6};
    fun(tab, 6);

    return 0;
}

Code::Blocks says that:

|49|error: invalid type argument of unary ‘*’ (have ‘int’)|
Brian Brown
  • 3,873
  • 16
  • 48
  • 79
  • in first code change `*(tab+1)` to `*(tab+i)`. and remove wsk. – vlastachu Dec 08 '13 at 21:29
  • Without "*`()` operator*" would mean what, please? – alk Dec 08 '13 at 21:33
  • Without `()`: `int * tabi = tab + i; printf("%d\n", *tabi);` (Not using parentheses as expression-grouping-operators, not using them at all would (probably) be impossible) – Kninnug Dec 08 '13 at 21:36
  • @alk ehm `*(tab+1)` are you about this? without `()` will changed operations priority so it'll executed as `*(tab)+1` it means: **1.** unname `tab` pointer with 0 shift - we get 0's element = 1; **2.** add 1 = 2; – vlastachu Dec 08 '13 at 21:40
  • @Kninnug: Ah, so all answers so far are wrong? – alk Dec 08 '13 at 21:40
  • @alk, why do you dicided so? To all: http://stackoverflow.com/questions/351409/appending-to-array js beginner question upvoted. C beginner question downvoted. – vlastachu Dec 08 '13 at 21:50
  • @alk I don't know that, but they do use parentheses where it's possible without. I upvoted your answer as it addresses both constraints as much as possible. – Kninnug Dec 08 '13 at 21:51
  • @vlastachu It wasn't me , at least not for the question! – alk Dec 08 '13 at 22:09
  • is it correct to say `() operator` when in c++ exist termin `() operator` which means `someObj(otherObj)`? Also if taken the question literally in that case it is impossible (write `printf` without parenthness). – vlastachu Dec 08 '13 at 22:13

4 Answers4

4

No grouping parentheses, nor subscript. No clutter:

void fun(int *tab, int n)
{
    while (n--)
        printf("%d\n", *tab++);
}
Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
3

Change

printf("%d\n", *(tab+1));

to

printf("%d\n", *(tab+i));

And in both programs:

int i, wsk = &tab;

should be:

int i, *wsk = tab;

EDIT: OP actually also has this constraint in the title without [] and () operators. Although one usually refers to the () operator for the cast operator, the () of the function call is also called the function-call operator in the C Standard and is listed with postfix operators in the Standard. Therefore I think this OP constraint is put the wrong way because without function call I doubt there is any valid answer to OP question.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • Without "*() operator*"? – alk Dec 08 '13 at 21:49
  • @alk there is no `()` operator in C other than the cast operator which is not used in this answer. If the no `()` operator actually meant without `()` then `printf` cannot be used and if no function call can be used, I doubt there is any solution to the question. – ouah Dec 08 '13 at 21:53
  • I fully agree about the "*() operator*" and was just try to provoke a comment ... sorry and thanks. @ouah – alk Dec 08 '13 at 22:07
  • 1
    @alk I think the OP constraint has been put the wrong way, I added an edit in my answer. – ouah Dec 08 '13 at 22:14
3

... without [] and () operators

It should be something like this then:

void fun(const int * tab, const size_t n)
{
  const int * wsk = tab;
  size_t i = 0;
  for(; i < n; ++i, ++wsk)
  {
      printf("%d\n", *wsk);
  }
}
alk
  • 69,737
  • 10
  • 105
  • 255
1

You want to print what tab is pointing to so do not use &tab

for(i=0; i<n; i++)
{
    printf("%d\n", *(tab+i));
}

in your first version you are always printing the second element, the *(tab+1) is basically "what tab is pointing to +1" which is the second element what you want is what tab is pointing to + a counter that increases by 1 each iteration, which is your i :)

edit: Sorry didnt notice that you didnt want () either. *tab++