Wakuu naombeni msaada wenu kugundua nini kimekosekana kwenye hii program yangu, najaribu kuicompile kutumia gcc filename -o filename but lakini naona inakubali lakini hainipi result ninazo taka mimi sasa sijui ni kwa nini au imekosekana nini.......hebu check hapa chini program yenyewe
//************************************************** *******
//point5.c - Demo of use of pointers. Pointers and functions.
//The function call passes arguments that are addresses of two variables x and y.
//The function, using the addresses of x and y modifies their content.
//The main function then displays the content of x and y.
//************************************************** ***********/
#include <stdio.h>
void addconst(int *px, int *py); // very clear function prototype
int main(void)
{
int x = 2, y = 4; //integer variables
printf("\n\npoint5.c Demo of pointers\r\n");
printf("=========================\r\n");
printf("Before function call :x = %d :y = %d\n\n", x , y);
printf("The function adds 3 to x and 6 to y indirectly by using the\n");
printf("addresses of the variables.\n\n");
//addconst( &x, &y ); // Addresses passed to function
printf("After function executed :x = d :y = d",x,y);
return 0;
}// end of main function
//addconst
//function is passed the addresses of two variables x and y
//and then uses these addresses to alter the content of the variables
void addconst( int *px , int *py)
{
*px = *px + 3;
*py = *py + 6;
}//end of function
Hiyo ndio program yenyewe sasa hapa nime compile kwa gcc point5.c -o point then haionyeshi error alafu naweka point5 kunionyesha result,lakini majibu ninayopata hapa sio ambayo mimi ninayotaka inanipa result tofauti ambayo ni
point5.c Demo of pointers
=========================
Before function call :x = 2 :y = 4
The function adds 3 to x and 6 to y indirectly by using the
addresses of the variables.
After function executed :x = d :y = d
so hapo nilipoweka red ilibidi jibu lake liwe kama x= 5 na y=10 lakini sasa inanipa result ya x=d and y=d so not really sure where nimeenda wrong kwenye hiyo program!
Kama mnaweza kuumiza kichwa naomba kufundishwa na kukosolewa au kama kuna kitu inabidi ni add ili niweze kupata hizo result nitashukuru sana...
Wakuu naombeni msaada wenu kugundua nini kimekosekana kwenye hii program yangu, najaribu kuicompile kutumia gcc filename -o filename but lakini naona inakubali lakini hainipi result ninazo taka mimi sasa sijui ni kwa nini au imekosekana nini.......hebu check hapa chini program yenyewe.......................................
Unaweza kunimbia hio line ina maana gani
void addconst(int *px, int *py) -- hivyo vinyota vina maana gani
Na kwa kwa nini hii function ya void addconst(int *px, int *py) umeiweka na kui declare kabla ya main funcntion na sio ndani au baada ya main()
NB.
Sio kwamba ninakukuosa ila napenda kujua kwa kweli ni mchovu wa C napenda kuchemsha kichwa tu
Hiyo * ina maanisha nini soory kukurudiha nyuma napenda kujikumbusha
hivyo vinyota ni pointer to variable, na hiyo line maana yake ni prototype function, in c programming function should be declared first the same as global variables kabla hujazitumia toofauti na language kama java ambayo huitaji ku declare methods mwanzoni. Hapo naona unapata wrong results sababu hiyo function call kwenye main function ume icomment kwa hiyo hakuna inachofanya,kwa hiyo kilichotokea imeprint line chini baada ya hiyo comment
printf("After function executed :x = d :y = d",x,y);
nadhani hio "d" kama ni string formating basi si ingekuwa na ki2 b4! kama variable haijawa declared!
Na sina hakika kama function itakuwa executed coz iko kwanye comments "//"!
sina compiler, so im sorry I cudnt try it!
Unaweza kunimbia hio line ina maana gani
void addconst(int *px, int *py) -- hivyo vinyota vina maana gani
Na kwa kwa nini hii function ya void addconst(int *px, int *py) umeiweka na kui declare kabla ya main funcntion na sio ndani au baada ya main()
Kama sikosei kwenye C wanasema function zote ni watoto wa main()
NB.
Sio kwamba ninakukuosoa ila napenda kujua .kwa kweli ni mchovu wa Cna ni siku nyingi sana napenda kuchemsha kichwa tu
Hiyo * ina maanisha nini sorry kukurudisha nyuma napenda kujikumbusha
Ok thank you very much je kwa hiyo ni sawa alivyofanya kuideclare kabla ya main()
//************************************************** *******
//point5.c - Demo of use of pointers. Pointers and functions.
//The function call passes arguments that are addresses of two variables x and y.
//The function, using the addresses of x and y modifies their content.
//The main function then displays the content of x and y.
//************************************************** ***********/
#include <stdio.h>
void addconst(int *px, int *py); // very clear function prototype
int main(void)
{
int x = 2, y = 4; //integer variables
printf("\n\npoint5.c Demo of pointers\r\n");
printf("=========================\r\n");
printf("Before function call :x = %d :y = %d\n\n", x , y);
printf("The function adds 3 to x and 6 to y indirectly by using the\n");
printf("addresses of the variables.\n\n");
addconst( &x, &y ); // Addresses passed to function
printf("After function executed :x = %d :y = %d",x,y);
return 0;
}// end of main function
//addconst
//function is passed the addresses of two variables x and y
//and then uses these addresses to alter the content of the variables
void addconst( int *px , int *py)
{
*px = *px + 3;
*py = *py + 6;
}//end of function
Wakuu naombeni msaada wenu kugundua nini kimekosekana kwenye hii program yangu, najaribu kuicompile kutumia gcc filename -o filename but lakini naona inakubali lakini hainipi result ninazo taka mimi sasa sijui ni kwa nini au imekosekana nini.......hebu check hapa chini program yenyewe
//************************************************** *******
//point5.c - Demo of use of pointers. Pointers and functions.
//The function call passes arguments that are addresses of two variables x and y.
//The function, using the addresses of x and y modifies their content.
//The main function then displays the content of x and y.
//************************************************** ***********/
#include <stdio.h>
void addconst(int *px, int *py); // very clear function prototype
int main(void)
{
int x = 2, y = 4; //integer variables
printf("\n\npoint5.c Demo of pointers\r\n");
printf("=========================\r\n");
printf("Before function call :x = %d :y = %d\n\n", x , y);
printf("The function adds 3 to x and 6 to y indirectly by using the\n");
printf("addresses of the variables.\n\n");
//addconst( &x, &y ); // Addresses passed to function
printf("After function executed :x = d :y = d",x,y);
return 0;
}// end of main function
//addconst
//function is passed the addresses of two variables x and y
//and then uses these addresses to alter the content of the variables
void addconst( int *px , int *py)
{
*px = *px + 3;
*py = *py + 6;
}//end of function
Hiyo ndio program yenyewe sasa hapa nime compile kwa gcc point5.c -o point then haionyeshi error alafu naweka point5 kunionyesha result,lakini majibu ninayopata hapa sio ambayo mimi ninayotaka inanipa result tofauti ambayo ni
point5.c Demo of pointers
=========================
Before function call :x = 2 :y = 4
The function adds 3 to x and 6 to y indirectly by using the
addresses of the variables.
After function executed :x = d :y = d
so hapo nilipoweka red ilibidi jibu lake liwe kama x= 5 na y=10 lakini sasa inanipa result ya x=d and y=d so not really sure where nimeenda wrong kwenye hiyo program!
Kama mnaweza kuumiza kichwa naomba kufundishwa na kukosolewa au kama kuna kitu inabidi ni add ili niweze kupata hizo result nitashukuru sana...
RedSilverDog said:sijawahi fanya C, this was my first time!.. lakini haina tafauti na C++ niliofanya..
void addConst(int& x, int& y){
//modify the x and y and they will be modified out of the function also
y=y+2;
x=x+5;
}
just ignore the quoted post. That will work with C++ but not C. OP is right on his way of using pointers. Here is a working codendugu,
nafikiri unahitaji ku brush kidogo C. Kifupi tu ni kuwa unapopitisha argument kama array ktk C/C++ compiler inapitisha pointer to the first element. Kwa hiyo ni kama unapitisha array of int. Pass the variables by reference if you want to modify them directly. You have just opened the can of worms!
you need to re write again. Just a note, simple program like this needs no comment. Next time just put in a code and whenever someone does not understand a line will askCode:void addConst(int& x, int& y){ //modify the x and y and they will be modified out of the function also y=y+2; x=x+5; }
#include <stdio.h>
void addConst(int* , int*);
int main(){
int x = 2, y = 4;
printf("Before function call x = %d y = %d\n\n", x , y);
printf("The function adds 3 to x and 6 to y indirectly by using the ");
printf("addresses of the variables.\n");
addConst(&x, &y);
printf("After function executed x = %d y = %d\n",x,y);
return 0;
}
void addConst(int* x, int* y){
*x = *x + 3;
*y = *y + 6;
}
Daaah! umenifanya nikatafute macompiler, so here it works!.. Haya ndo maswali ya kuuliza.. :ranger:
Mkuu ndio hivyo inabidi kusaidiana kimtindo na nashukuru sana kwa moyo wako na program inafanya kazi vizuri kabisa na ina work bila tatizo....Pia mkuu kama inawezekana unaweza ukawapa wadau na wenyewe maana wengine hawana compiler kwa ajili ya hiyo program so ungeweza labda kuwajulisha wewe unatumia nini kui compile...........
sijawahi fanya C, this was my first time!.. lakini haina tafauti na C++ niliofanya..
Wakuu mimi sio mtu wa C, lakini natatizwa na utumiaji wa one variable d (not a multvariable attribute) na somehow tutegemee irudishe two different values :x = d :y = d ?!
Mkuu ni kweli ndio hapo wakuu hapo juu wamekuja kufind out kwamba hiyo ndio problem iliyokuwepo kwenye hiyo program ila kila kitu kingine ni sawa kabisa...........
umedeclare int x =2, na y = 4
then x = x + 3 // 5
y = y + 6 //10
ni vipi utapata different results wakati :x = d :y = d
- mbona addconstant() method umeicomment out?
Hiyo ipo fine haiwezi kuleta tatizo mkuu ni sawa hapo ilivyo.......
- hapa naona unadisplay tu x and y the rest in "message" printf("Before function call :x = %d :y = %d\n\n", x , y);
Iangalie tena program yako, error itakuwepo kama kuna syntax issue otherwise cha msingi je unapata output uliyotegemea.
haya ndo mambo ngoja niistall turbo borland C++ nami nijifunze
mkubwa japo nami sio mtaalma wa prg cha muhimu sio program itoejibu sahihi cha muhimu program logic iwe sahihi. kwahiyo usisitize prog ikupe jibu 10 tu.
tupe somo program logic yake ilikuwa na kosa wapi?
tupe somo program logic yake ilikuwa na kosa wapi?