Home > other >  Changing the value of an element in a struct
Changing the value of an element in a struct

Time:03-13

I'm new to structs. I am trying to write a program that has a struct, and the struct is supposed to store a character array and its length. I want to be able change the length's value as I would be creating functions like trimming/concatenating the array. Here is a code I wrote:

#include <stdio.h>
#include <stdlib.h>
struct strstruct{
unsigned int length;
char string[20];
};
typedef struct strstruct stru;
int strleng(stru A){
  int i=0;
  while(A.string[i]!='\0'){
    i  ;
  }
  A.length =i;
  return i;
}
int main(){

  stru A = {1,
 {'a','b','c','d','e','f'}
  };
  
  printf("%d %d\n",strleng(A),A.length);
  return 0;
}

The value of A.length is not changing inspite of calling strleng.
(i)Why?
(ii) Is there another way to do it?

CodePudding user response:

For starters the order of evaluation of arguments in a function call is unspecified.

So in this call

 printf("%d %d\n",strleng(A),A.length);

the evaluation of the argument expression A.length can occur before calling the function strleng or vice versa.

Secondly the function strleng declared like

int strleng(stru A);

deals with a copy of the original object A declared in main and used as an argument. So changing the copy does not influence on the original object.

You need to pass the object by reference through a pointer to it.

unsigned int strleng( stru *A){
  unsigned int i=0;
  while(A->string[i]!='\0'){
    i  ;
  }
  A->length =i;
  return i;
}

and in main you should write for example

unsigned int n = strleng( &A );
printf("%u %u\n", n, A.length );

Pay attention to that on one hand, the data member length is declared as having the type unsigned int

unsigned int length;

On the other hand, within your original function strleng you are using an object of the signed type int and the function return type is also int. The function should use at least the same type unsigned int instead of the type int.

CodePudding user response:

Try the code below:

#include <stdio.h>
#include <stdlib.h>
struct strstruct{
unsigned int length;
char string[20];
};
typedef struct strstruct stru;
int strleng(stru* A){
  int i=0;
  while(A->string[i]!='\0'){
    i  ;
  }
  A->length =i;
  return i;
}
int main(){

  stru A = {1,
 {'a','b','c','d','e','f'}
  };
  
  printf("%d %d %d\n",A.length, strleng(&A),A.length);
  printf("%d \n",A.length);
  return 0;
}

You will get output: 6 6 1. I should get the answer now.

At first, you need to use pointer as a parameter if you want to modify struture's value inner a fucntion.

For your question:

  • To most of the c compiler, the functions inner a printf function is prcessed from right to left. I think the compiler in your case is this one.
  • For some c compiler, it do process functions in one line from left to right.

I hope it can help you, c online compiler: https://www.onlinegdb.com/online_c_compiler.

CodePudding user response:

printf("%d %d\n",strleng(A),A.length);
  1. Firstly, Here you are passing the argument to the strleng function as a value means strleng function' parameter is a copy of A. In other words, variable A in the main function and structure variable inside the strleng function are two independent variables. So changing A.length in the strleng function will not be visible to your variable A in the main function. (There are many good online resources available about Pass by value vs. Pass by reference. You can check those for better understanding)
  2. Most of the compilers takes each parameter of printf() from right to left. So here A.length execute first then strleng(A). So even you pass the argument by reference, it will still output 6 1.

Updated Code

#include <stdio.h>
#include <stdlib.h>
struct strstruct {
    unsigned int length;
    char string[20];
};
typedef struct strstruct stru;
int strleng(stru* A) {
    int i = 0;
    while(A->string[i] != '\0'){
    i  ;
    }
    A->length = i;
    return i;
}
int main() {

    stru A = {1, {'a','b','c','d','e','f'}};

    printf("%d %d %d\n", A.length, strleng(&A), A.length);//6 6 1
    return 0;
}
  • Related