Похожие чаты

Guys Im passing an array to a function The function defination is

like this:
int my_funcie(int *arr);

It takes an array of integers, and let's say it prints the array's elements

The problem is I want to use a for loop to print elements, and I don't want to hardcode the condition
For example

for(int index = 0; index < array_length; index++)

here, the array_size is the problem
According to this StackOverflow answer

It should work using this piece of code:
int array_length = sizeof(arr) / sizeof(arr[0])

I put this inside my my_funcie function, but it doesn't give me the proper result
For example It's saying the array with 5 integer elemnts (sizeof(arr)) is 8 and the sizeof(arr[0]) is 4
It does 8/4 and gets 2 as the array_length

What am I doing wrong?
The same method works pretty well where I defined the array itself

I'm using C and gcc 9.2.0 btw

3 ответов

5 просмотров

It doesn't work this way if you're getting the array as a function parameter. Go through the ss posted by John it'll explain this in detail. Instead of array you can use vectors. Personally I prefer vectors over arrays plus vectors have their own size() function which will give you an exact count of the number of elements present inside it.

rewrite the function signature as int my_funcie(size_t size, int arr[size])

Jens Gustedt's Modern C has multiple paragraphs about the difference between arrays and pointers. you should read them. the book is linked in the pinned message.

Похожие вопросы

Обсуждают сегодня

Всем привет, написал код ниже, но он выдает сегфолт, в чем причина? #include <stdio.h> #include <stdlib.h> #include <string.h> struct product { char *name; float price; };...
buzz базз
75
Hey, Oceaners 🌊 As we approach Phase 1 of the token merger, OCEAN and AGIX will migrate to FET starting July 1st! Here's a list of exchanges that have communicated their su...
KreigDK | Never DM first🌊
3
База данных не поможет. Шифрование не поможет. Какие там ещё варианты? Накидывайте.
КТ315
20
I love the passion, really do. I do think this is being blown way out of proportion. We are not deleting our Telegram or not allowing you to talk about anything you want, a...
Mike Herron
3
Did you guys see the latest tweet from TonGifts? 🚀
Mike
44
А как лучше конвертировать физический адрес в виртуальный при маппинге? В случае ядра у меня, например, direct mapping, первые 768МБ я как есть мапплю в higher half, а остальн...
Evg Resh
26
А табстоп это сообщение от окна или от элемента управления?
The Bird of Hermes
18
Hey, what is the best and maintained way of transpiling cpp to c? (As a need to support many different toolchains quickly and may not have a c++ support in the given toolch...
Meitar Reihan
11
Открыл свой двухкилобайтный экзешник в x32dbg, а тут какая-то хрень. Смущает кнопка "выполнить до пользовательского кода", а что ещё может быть в файле помимо него ?
НѣкъиⰘижєжєиꙁъвьсєсвѣтьноѣсѣтиѥсть•
11
Hey guys, did you see the latest announcement about $FUN? 🔥
Brian
21
Карта сайта