1

I have learned how to use functions and structs and pointers. I want to combined them all into one. But the code that I write doesn't seem to work. The compiler tells me the test is an undeclared identifier. Here is the code:

#include <stdio.h>
#include <stdlib.h>

struct character 
{
  int *power;
};

void test (use_power)

int main ()
{
  test (use_power)
  printf("%d\n",*power);

  return 0;
}

void test () 
{
  int use_power = 25;
  struct character a;
  a.power = &use_power;
}
8
  • 2
    What is this code?, it's completely wrong, it's wrong in many ways and it's just a little of code. Commented May 12, 2016 at 23:37
  • "The compiler tells me the test is an undeclared identifier."... That's all it tells you? Commented May 12, 2016 at 23:40
  • @DigitalNinja It has to stop at the first error, all the program is an error. Commented May 12, 2016 at 23:41
  • @DigitalNinja yes. Commented May 12, 2016 at 23:41
  • 1
    What do you think the line void test use_power() is doing? One of test and use_power is an interloper (probably use_power, for all the compiler mentions test), and there's a semicolon missing too. Continue from there, one error message at a time. If you must ask about compiler error messages, then please include them in the question — at least, include the first one; the others may be consequences of the first. Up to about 5 error messages would be OK; more than that is too many. Include the file names for the error messages, but leave out long paths so that they're readable. Commented May 12, 2016 at 23:45

3 Answers 3

1

Your code has many mistakes it can't even compile

  1. Multiple missing semicolons.
  2. Implicit declaration of test() here

    test (use_power)
    

    with a missing semicolon too.

  3. power is not declared in main().

  4. This line

    void test use_power()
    

    does not make sense and is invalid, and also has no semicolon.

  5. The a instance in test() defined at the end is local to test() and as such will be deallocated when test() returns. The use_power int, has exactly the same problem and trying to extract it's address from the function is useless because you can't access it after the function has returned.

I have no idea what you were trying to do, but this might be?

#include <stdio.h>
#include <stdlib.h>

struct character {
    int *power;
};

/* Decalre the function here, before calling it
 * or perhaps move the definition here
 */
void test(struct character *pointer);
/*                                  ^ please */

int
main(void) /* int main() is not really a valid signature */
{
    struct character instance;
    test(&instance);
    if (instance.power == NULL)
        return -1;
    printf("%d\n", *instance.power);
    free(instance.power);
    return 0;
}

void
test(struct character *pointer)
{
    pointer->power = malloc(sizeof(*pointer->power));
    if (pointer->power != NULL)
        *pointer->power = 25;
}
Sign up to request clarification or add additional context in comments.

5 Comments

test is the name of a function. I think he meant to forward declare a function called test that takes a parameter called use_power. Unfortunately when he actually defines test it doesn't take a parameter but it does modify the non existent use_power parameter. Or maybe I have that wrong - its hard to tell.
I think maybe the answer is to delete this line altogether: void test use_power() and remove the use_power parameter from the test function call in main. Then at least it makes sense.
ok... But can't you use pointers to get values outside of the scope of void test? I'm trying to to declare a pointer in struct. Use that pointer in a function. And then print that result in the main function.
No, because they have automatic storage and will be deleted after the function has returned, you can use static which would give it static storage, but it's highly unlikely that you want that.
@iharob yes, the code you wrote is what I was trying to do. Thank you so much! Now I can analyze it and figure out the rest. Thanks again!
0

Your code seems to be wrong. Your definition for test contains no arguments as

void test () 
{
  int use_power = 25;
  struct character a;
  a.power = &use_power;
}

but your prototype contains one argument

void test (use_power)

which is wrongly put. First there are no semicolons; at the end of your prototype declaration, secondly by looking at your code, use_power is a variable and not a datatype so it cannot be present solely in a function declaration.

You will get an argument mismatch error.

You have used the line in main()

printf("%d\n",*power);

which is absolutely wrong. you cannot access any member of a structure without a structure variable.

And again, you have not mentioned the; after your call to the incorrect test()before this line

As you have not put your question so properly, I must figure out what you wish to achieve. I bet you want to hold the address of a integer in the pointer member of a structure and then print its value.

Below is a code snippet which will work as you desire.

#include <stdio.h>
#include <stdlib.h>

struct character 
{
  int *power;
};
struct character a;    //define a structure variable

void test ();

int main ()
{
  test ();
  printf("%d\n",*(a.power)); // print the member of structure variable a 

  return 0; 
}

void test () 
{
  int use_power = 25;
  a.power = &use_power;
}

6 Comments

Thanks so much! This is EXACTLY what I was looking for! Thanks again!
I didn't know I could do that. I would have done so early if I had known!
@BLUEPIXY did you compile?
It does not matter whether you did compile.
@BLUEPIXY ok fine. Can i know what kind of undefined behavior? I couldn't get it.
|
0

example

#include <stdio.h>

struct character {
    int *power;
};

void test(struct character *var);

int main (void){
    struct character use_power;
    int power = 5;

    use_power.power = &power;
    test(&use_power);
    printf("%d\n", power);

    return 0;
}

void test(struct character *var){
    int use_power = *var->power;
    *var->power = use_power * use_power;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.