Author Topic: Help me / C  (Read 1097 times)

0 Members and 1 Guest are viewing this topic.

[TKC]ViruS

  • Klass Klown
  • ***
  • Posts: 367
  • TKC-COMMUNITY.NET
    • View Profile
Help me / C
« on: June 11, 2012, 06:46:28 pm »
Hi guys.
I got some work in C language, but i have one problem..I have three files, main and function.c + macros.h

Function.c -  there are all functions of program
main.c - only main function


i have to use pointers, arrays, functions with a parameter.
OK now:
I have problem with one function with a parameter, here is:


void priklad1(float x, float y, float z, float v)
{
    FILE *fr;

 
    float *p_x;
    float *p_y;
    float *p_z;
    float *p_v;

    p_x=&x;
    p_y=&y;
    p_z=&z;
    p_v=&v;
    fr=fopen("vysledek.txt", "w");

    printf("\n\t\t\t\tZadejte R1: _\b");
    scanf("%f", &x);
    printf("\t\t\t\tZadejte R2: _\b");
    scanf("%f", &y);
    printf("\t\t\t\tZadejte R3: _\b");
    scanf("%f", &z);
    printf("\t\t\t\tZadejte U: _\b");
    scanf("%f", &v);
    printf("\n\t\t\t\tRC = ?\n\t\t\t\tI = ?\n");
    printf("\t\t\t-- ----------------------------");
    printf("\n\t\t\t\[VYSLEDEK]\n");
    printf("\n\t\t\t\t  RC: %.2f\n", *p_x+*p_y+*p_z);
    printf("\t\t\t\t   I: %.2f\n", *p_v/(*p_x+*p_y+*p_z));
    fclose(fr);
}


I want to make one variable than x,y,z,v so = float xxx[4], but how to make function with parameters with this array + pointers?
Sorry for this stupid post, Thanks for help

M. O.

  • Administrator
  • MasstKer
  • *
  • *
  • *
  • Posts: 9166
    • View Profile
    • http://www.tkc-community.net
Re: Help me / C
« Reply #1 on: June 11, 2012, 07:47:20 pm »
Ok, you can make a

void priklad1(float *params)
{

   float x = params[0] etc






Heckling is an art, and game hacking a science.

[TKC]ViruS

  • Klass Klown
  • ***
  • Posts: 367
  • TKC-COMMUNITY.NET
    • View Profile
Re: Help me / C
« Reply #2 on: June 11, 2012, 08:00:53 pm »
Ok, you can make a

void priklad1(float *params)
{

   float x = params[0] etc

Ok, now:

void priklad1(float *params)
{
    FILE *fr;

    float x = params[0];
    float y = params[1];
    float z = params[2];
    float v = params[3];
    float *p_x;
    float *p_y;
    float *p_z;
    float *p_v;


    p_x=&params[0];
    p_y=&params[1];
    p_z=&params[2];
    p_v=&params[3];
    fr=fopen("vysledek.txt", "w");

    printf("\n\t\t\t\tZadejte R1: _\b");
    scanf("%f", &params[0]);
    printf("\t\t\t\tZadejte R2: _\b");
    scanf("%f", &params[1]);
    printf("\t\t\t\tZadejte R3: _\b");
    scanf("%f", &params[2]);
    printf("\t\t\t\tZadejte U: _\b");
    scanf("%f", &params[3]);
    printf("\n\t\t\t\tRC = ?\n\t\t\t\tI = ?\n");
    printf("\t\t\t-- ----------------------------");
    printf("\n\t\t\t\[VYSLEDEK]\n");
    printf("\n\t\t\t\t  RC: %.2f\n", *p_x+*p_y+*p_z);
    printf("\t\t\t\t   I: %.2f\n", *p_v/(*p_x+*p_y+*p_z));
    fclose(fr); // ZAVRENI SOUBORU
}

but how i have to change the function in the main file?








M. O.

  • Administrator
  • MasstKer
  • *
  • *
  • *
  • Posts: 9166
    • View Profile
    • http://www.tkc-community.net
Re: Help me / C
« Reply #3 on: June 11, 2012, 08:15:47 pm »
Yes, you have to declare

float params[4]

and then load the values into it.

params[0] = a etc.

then pass it to the procedure

priklad(params)



Heckling is an art, and game hacking a science.

[TKC]ViruS

  • Klass Klown
  • ***
  • Posts: 367
  • TKC-COMMUNITY.NET
    • View Profile
Re: Help me / C
« Reply #4 on: June 11, 2012, 08:32:16 pm »
Yes, you have to declare

float params[4]

and then load the values into it.

params[0] = a etc.

then pass it to the procedure

priklad(params)

Mullah, I cant make "params[0] = a" because in function.c i have:

void priklad1(float *params)
{
}


So,
BEFORE

file function.c
_______________
void priklad1(float x, float y, float z, float v)
{
}

file main.c
_______________

float a,b,c,d;

switch(getchar())
    {
        case '1':
        {
            priklad1(a,b,c,d);
            system("pause");
            break;
        }

AFTER

file function.c
_______________

void priklad1(float *params)
{
}

but file main.c is the same as before, understand :(? we have to change MAIN file yet..

change float a,b,c,d;
and priklad1(a,b,c,d) in switch

Coronel_Niel

  • Insane Joker
  • ****
  • Posts: 846
  • Why can't I pick my own profile picture...
    • View Profile
Re: Help me / C
« Reply #5 on: June 11, 2012, 08:37:50 pm »
Yes, you have to declare

float params[4]

and then load the values into it.

params[0] = a etc.

then pass it to the procedure

priklad(params)

Mullah, I cant make "params[0] = a" because in function.c i have:

void priklad1(float *params)
{
}


So,
BEFORE

file function.c
_______________
void priklad1(float x, float y, float z, float v)
{
}

file main.c
_______________

float a,b,c,d;

switch(getchar())
    {
        case '1':
        {
            priklad1(a,b,c,d);
            system("pause");
            break;
        }

AFTER

file function.c
_______________

void priklad1(float *params)
{
}

but file main.c is the same as before, understand :(? we have to change MAIN file yet..

change float a,b,c,d;
and priklad1(a,b,c,d) in switch

float params[4];
params[0]=&a;
params[1]=&b;
params[2]=&c;
params[3]=&d;

priklad1(params)

pass params to the function as it is and have it hold the memory address's of the values
"Now we are going to watch my boys do it" - Joopig

[TKC]ViruS

  • Klass Klown
  • ***
  • Posts: 367
  • TKC-COMMUNITY.NET
    • View Profile
Re: Help me / C
« Reply #6 on: June 11, 2012, 08:49:21 pm »
hey hey nice work guys, it works! im happy now! thanks so much!
 :icon_magician

M. O.

  • Administrator
  • MasstKer
  • *
  • *
  • *
  • Posts: 9166
    • View Profile
    • http://www.tkc-community.net
Re: Help me / C
« Reply #7 on: June 11, 2012, 08:53:13 pm »
Param is a local copy, but will hold the same address of the variables to be accessed.


But I don't think you want &a there. param is an address, but param[0] is a value.
Heckling is an art, and game hacking a science.

Coronel_Niel

  • Insane Joker
  • ****
  • Posts: 846
  • Why can't I pick my own profile picture...
    • View Profile
Re: Help me / C
« Reply #8 on: June 11, 2012, 09:03:09 pm »
In C++, AFAIK, de-referancing(&) a value will return its memory address as a value. Then params[0] holds the value of a memory address, which the program can access. I suppose compiles know that holds the address of a float and doesn't complain.

Putting the address into the array means you can edit it while inside the function and it will be edited on the return. If this isn't what you want todo then change it ASAP, you will run into problems later.
"Now we are going to watch my boys do it" - Joopig

[TKC]ViruS

  • Klass Klown
  • ***
  • Posts: 367
  • TKC-COMMUNITY.NET
    • View Profile
Re: Help me / C
« Reply #9 on: June 11, 2012, 09:06:51 pm »
I know that array[0] is value, i didn't know how to change values in main.c file. I am glad it works now

+1 Mullah
+1 Coronel

Thanks