![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
doguhanpala
Joined: 05 Oct 2016 Posts: 120
|
array size change after parameter sending (SOLVED) |
Posted: Wed Mar 08, 2017 2:35 am |
|
|
Hello everyone
I wrote a code play a melody. It works successfully. I want to use the 3 arrays (dizi, bosluk, notauzunluk) as parameter to notacal() function. The problem is, after sending the array to function its size changes and decreases to 2. I use 18f2550.
These are the codes.
This one is working good:
Code: | #include <18F2550.h>
#DEVICE ADC=10
#fuses INTRC_IO,HS, NOWDT, NOLVP, HSPLL,NOMCLR,NOPROTECT,NODEBUG,NOBROWNOUT,USBDIV,PLL5,CPUDIV1,VREGEN,NOPUT
#use delay(clock=8000000)
#define voice PIN_A1
#include <math.h>
int16 A=568; //1
int16 Ad=536; //2
int16 B=506; //3
int16 C=955; //4
int16 Cd=901; //5
int16 D=851; //6
int16 Dd=804; //7
int16 E=758; //8
int16 F=715; //9
int16 Fd=675; //10
int16 G=634; //11
int16 Gd=602; //12
int16 A4=1136; //13
int16 Ad4=1072; //14
int16 B4=1012; //15
int16 C4=1911; //16
int16 Cd4=1803; //17
int16 D4=1702; //18
int16 Dd4=1607; //19
int16 E4=1516; //20
int16 F4=1431; //21
int16 Fd4=1351; //22
int16 G4=1275; //23
int16 Gd4=1203; //24
//!int8 dizi[]= {15,14,13,24}; //fail melody 15,14,13,24
//!int16 bosluk[]= {0,0,0,0}; //notalar arasındaki bekleme süresi
//!int16 notauzunluk[]= {500,500,500,2000}; //notaların çalınma süresi
//!
int8 dizi[]= {11,4,7,9,11,4,7,9,6,9,14,7,6,9,14,7,6,4}; //fail melody 15,14,13,24
int16 bosluk[]= {50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50}; //notalar arasındaki bekleme süresi
int16 notauzunluk[]= {2000,2000,200,200,1000,1000,200,200,4000,2000,2000,200,200,1000,1000,200,200,4000}; //notaların çalınma süresi
//!
int vurus=1;
int16 k;
int16 i;
int16 duration=0;
float nota=0;
float temp=0;
//notalar
void pausesong()
{
output_low(voice);
delay_us(vurus);
}
void playnote(long note)
{
output_high(voice);
delay_us(note);
output_low(voice);
delay_us(note);
}
void main(){
setup_oscillator(OSC_8MHz);
while(1){
delay_ms(1000);
for (k=0; k<=sizeof(dizi)-1; k++)
{
switch(dizi[k])
{
case 0: pausesong(); break;
case 1: nota=A; break;
case 2: nota=Ad; break;
case 3: nota=B; break;
case 4: nota=C; break;
case 5: nota=Cd; break;
case 6: nota=D; break;
case 7: nota=Dd; break;
case 8: nota=E; break;
case 9: nota=F; break;
case 10: nota=Fd; break;
case 11: nota=G; break;
case 12: nota=Gd; break;
case 13: nota=A4; break;
case 14: nota=Ad4; break;
case 15: nota=B4; break;
case 16: nota=C4; break;
case 17: nota=Cd4; break;
case 18: nota=D4; break;
case 19: nota=Dd4; break;
case 20: nota=E4; break;
case 21: nota=F4; break;
case 22: nota=Fd4; break;
case 23: nota=G4; break;
case 24: nota=Gd4; break;
}
temp=nota/1000;
duration=notauzunluk[k]/(temp*2);
for(i=0;i<(duration/2);i++)
{
playnote(nota);
}
delay_ms(bosluk[k]);
}
}
} |
So i wrote this code. I tried to send the arrays as parameters. I want to be able to choose a melody without commenting and uncommenting dizi, bosluk, notavurus arrays
Code: |
#include <18F2550.h>
#fuses INTRC_IO,NOWDT, PUT,NOMCLR,NOPROTECT,NOLVP,NODEBUG,NOBROWNOUT,CPUDIV1,VREGEN
#device ADC=10
#use delay(clock=8000000)
#use rs232(baud=9600,xmit=PIN_B7,rcv=PIN_B6)
#define voice PIN_A1
#include <math.h>
int16 A=568; //1
int16 Ad=536; //2
int16 B=506; //3
int16 C=955; //4
int16 Cd=901; //5
int16 D=851; //6
int16 Dd=804; //7
int16 E=758; //8
int16 F=715; //9
int16 Fd=675; //10
int16 G=634; //11
int16 Gd=602; //12
int16 A4=1136; //13
int16 Ad4=1072; //14
int16 B4=1012; //15
int16 C4=1911; //16
int16 Cd4=1803; //17
int16 D4=1702; //18
int16 Dd4=1607; //19
int16 E4=1516; //20
int16 F4=1431; //21
int16 Fd4=1351; //22
int16 G4=1275; //23
int16 Gd4=1203; //24
int8 dizi1[]= {15,14,13,24}; //fail melody 15,14,13,24
int16 bosluk1[]= {0,0,0,0}; //notalar arasındaki bekleme süresi
int16 notauzunluk1[]= {500,500,500,2000}; //notaların çalınma süresi
int8 dizi2[] = {24,21,3,12}; //başarı müziği
int16 bosluk2[]= {0,0,0,0}; //notalar arasındaki bekleme süresi
int16 notauzunluk2[]= {300,600,400,300}; //notaların çalınma süresi
int vurus=1;
int16 k;
int16 i;
int16 duration=0;
float nota=0;
float temp=0;
void pausesong();
void playnote(long);
void notacal(int8 dizi[],int16 bosluk[],int16 notauzunluk[]);
void main()
{
SETUP_OSCILLATOR(OSC_8MHZ);
while(TRUE)
{
notacal(dizi2,bosluk2,notauzunluk2); //size of dizi2 is 4
}
}
void pausesong()
{
output_low(voice);
delay_us(vurus);
}
void playnote(long note)
{
output_high(voice);
delay_us(note);
output_low(voice);
delay_us(note);
}
void notacal(int dizi[], int16 bosluk[], int16 notauzunluk[])
{
for (k=0; k<=sizeof(dizi)-1; k++)
{
printf("dizi = %d ",sizeof(dizi)); // i print the size of array and it is 2
switch(dizi[k])
{
case 0: pausesong(); break;
case 1: nota=A; break;
case 2: nota=Ad; break;
case 3: nota=B; break;
case 4: nota=C; break;
case 5: nota=Cd; break;
case 6: nota=D; break;
case 7: nota=Dd; break;
case 8: nota=E; break;
case 9: nota=F; break;
case 10: nota=Fd; break;
case 11: nota=G; break;
case 12: nota=Gd; break;
case 13: nota=A4; break;
case 14: nota=Ad4; break;
case 15: nota=B4; break;
case 16: nota=C4; break;
case 17: nota=Cd4; break;
case 18: nota=D4; break;
case 19: nota=Dd4; break;
case 20: nota=E4; break;
case 21: nota=F4; break;
case 22: nota=Fd4; break;
case 23: nota=G4; break;
case 24: nota=Gd4; break;
}
temp=nota/1000;
duration=notauzunluk[k]/(temp*2);
for(i=0;i<(duration/2);i++)
{
playnote(nota);
}
delay_ms(bosluk[k]);
}
}
|
The size of dizi2 is 4 but after sending it to the notacal function it becomes 2.
I thought i am making a mistake at sending the array to function but it says here i am doing it right.
https://www.tutorialspoint.com/cprogramming/c_passing_arrays_to_functions.html
What am i missing?
Thank you so much.
Best wishes
Doguhan
Last edited by doguhanpala on Wed Mar 08, 2017 2:57 am; edited 1 time in total |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19360
|
|
Posted: Wed Mar 08, 2017 2:44 am |
|
|
size....
An array passed to a function _does not have a size_. What is passed is a 'pointer', which no longer 'knows' the size of the array. Sizeof this pointer, returns the size of the pointer, _not_ the array. Note in the code you point to, how they pass the 'size' of the array to the function as well.
You need to do the same. So:
Code: |
notacal(dizi2,sizeof(dizi2), bosluk2,notauzunluk2); //size of dizi2 is 4
//then in the function:
void notacal(int dizi[],int16 size, int16 bosluk[],int16 notauzunluk[])
{
for (k=0; k<=size-1; k++)
{
printf("dizi = %d ",size);
|
|
|
![](templates/subSilver/images/spacer.gif) |
doguhanpala
Joined: 05 Oct 2016 Posts: 120
|
|
Posted: Wed Mar 08, 2017 2:56 am |
|
|
Ttelmah wrote: | size....
An array passed to a function _does not have a size_. What is passed is a 'pointer', which no longer 'knows' the size of the array. Sizeof this pointer, returns the size of the pointer, _not_ the array. Note in the code you point to, how they pass the 'size' of the array to the function as well.
You need to do the same. So:
Code: |
notacal(dizi2,sizeof(dizi2), bosluk2,notauzunluk2); //size of dizi2 is 4
//then in the function:
void notacal(int dizi[],int16 size, int16 bosluk[],int16 notauzunluk[])
{
for (k=0; k<=size-1; k++)
{
printf("dizi = %d ",size);
|
|
Thank you so much Ttelmah! |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19360
|
|
Posted: Wed Mar 08, 2017 3:05 am |
|
|
Thanks.
It's a 'get your head round' difference in C. Many languages now have an 'array' as a type, and when an array is passed, what is passed, includes the 'size' information. C doesn't do this, so problems like yours appear....
Well done, on writing code to try to see 'what was happening'. Adding the print to see what size was reported etc.. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|