1

I have recently read a code but I have a doubt in code

toTs *now = try();
now->index = 30;

Where toTs is a struct and try() is a function with return type toTs*

*now being a pointer can keep address of label try() but as try() not being a structure variable now can't access it like struct and can never access it like now->index=30.

After compiling it shows segmentation fault.
I just want to ask is above code legitimate or not.

#include <stdio.h>
#include <unistd.h>

typedef struct toT {
    int index;
} toTs;

toTs lst[3];

toTs *try() {
    int i;
    for (i = 0; i < 3; i++) {
        toTs *current = &lst[i];
        printf("%d\n", current->index);
        if (current->index == 3) {
            printf("test work");
            return current;
        }
    }
}

int main() {
    int i;

    for (i = 0; i < 3; i++) {
        lst[i].index = i;
    }

    for (i = 0; i < 3; i++) {
        printf("test %d\n", lst[i].index);
    }

    toTs *now = try();
    now->index = 30;

    printf("current %d\n", now->index);
    printf("current %d\n", lst[2].index);
}

now is a struct pointer that can point to struct variable but try() is not a struct variable nor array of data structure its a function

9
  • 1
    Where's the definition of toTs & try()? Commented Feb 25, 2022 at 6:01
  • try is not a keyword/reserved-word in C, so no conflict there. You need to allocate memory for now inside try() or before calling try(). Commented Feb 25, 2022 at 6:08
  • Sorry i think i was not able to make you understand what i wanted to tell Commented Feb 25, 2022 at 6:10
  • Share the full definition of try() function code. Commented Feb 25, 2022 at 6:15
  • ok @SparKot ,thanks for giving time for my doubts Commented Feb 25, 2022 at 6:17

2 Answers 2

1

You need to always return a valid toTs* from try() for the code to work.

#include <stdio.h>
#include <unistd.h>

typedef struct toT {
    int index;
} toTs;

toTs lst[4];

toTs *try() {
    int i;
    for (i = 0; i < 3; i++) {
        toTs *current = &lst[i];
        printf("%d\n", current->index);
        if (current->index == 3) {
            printf("test work");
            return current;
        }
    }
    return &lst[3]; // return a spare structure.
}

int main() {
    int i;

    for (i = 0; i < 3; i++) {
        lst[i].index = i;
    }

    for (i = 0; i < 3; i++) {
        printf("test %d\n", lst[i].index);
    }

    toTs *now = try();
    now->index = 30;

    printf("current %d\n", now->index);
    for (i = 0; i < 4; i++) {
        printf("Final %d\n", lst[i].index);
    }
}

You're confusing function pointers with plain pointers in C. Function pointers go deeper : https://en.wikipedia.org/wiki/Function_pointer

Sign up to request clarification or add additional context in comments.

Comments

0

This code is not particularly healthy.

Given the way lst is initialized, when you call try() it will reach the end of that function without a return statement.

The C Standard says that if you use the return value of a function, without actually returning a value, that's undefined behavior.

3 Comments

Sorry if i have wasted your time but please forget everything about code and just tell me how this is possible "toTs *now = try(); now->index = 30;" a struct pointer pointing to a function and than writing "now->index=30" there isn't it accessing area of code
Struct pointer is not pointing to a function. A function is returning a struct pointer to save in a struct pointer variable.
Thanks a lot this was my issue @SparKot and you solved it.

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.