Search code examples
cbashgdbcentos7cc

How could calling another function fill in the elements of the array in C?


• I am struggling to understand the program that reads a set of text lines and prints the longest • As per K&R C (1.9), snippet:

int my_getline(char line[], int maxline); /* my_getline should return only integer (not string) */
void copy(char to[], char from[]);
int main()
{
    int len; /* current line length */
    int max; /* maximum length seen so far */
    char line[MAXLINE]; /* current input line */
    char longest[MAXLINE]; /* longest line saved here */

    max=0;
    while ((len=my_getline(line,MAXLINE))>0)
        if (len>max) {
            max=len;
            copy(longest,line);
        }
    if (max>0) /* there was a line */
        printf("%s",longest); /* how can array 'longest' got filed with values */
    return 0;
} 

• Hence, I have created another program, to simplify it.

Here is my source code:

#include <stdio.h>
#define MAX 100
int function(char i[], int);

int main() {
    char array1[MAX];
    function(array1,MAX);
    /* Using printf to print the 1st argument of function, which is array */
    printf("\nThis is THE STRING: %s\n", array1);
}

int function(char s[],int lim) {
    int c,i;
    for (i=0;i<lim-1&&(c=getchar())!=EOF&&c!='\n';++i)
        s[i]=c;
    return i; /* Does it mean, that the 'function' returns only integer 'i'? */
}

When compiling the program and executing:

pi@host:~/new$ cc -g test.c
pi@host:~/new$ a.out
hello, world

This is THE STRING: hello, worldvL±~ó°~£Cnr<€v@ v@¶~\áv=
pi@host:~/new$

When I have tried to debug on the same using gdb:

(gdb) run
Starting program: /home/pi/new/a.out 
Breakpoint 1, function (s=0x7efff574 "\304\365\377~\300\365\377~", lim=100)
at test.c:14
14      for (i=0;i<lim-1&&(c=getchar())!=EOF&&c!='\n';++i)
(gdb) n
Hello, world

Breakpoint 3, function (s=0x7efff574 "\304\365\377~\300\365\377~", lim=100)
at test.c:15
15      s[i]=c;
(gdb) p s
$1 = 0x7efff574 "\304\365\377~\300\365\377~"
(gdb) p i
$2 = 0
(gdb) p c
$3 = 72
(gdb)    

Questions:

  1. How can the elements of the array 'array1' be filled in with the values, if the function, in which all of that is taking place, which is 'function' is capable of returning only integer?

  2. Why the output string is added with "vL±~ó°~£Cnr<€v@ v@¶~\áv="?

  3. What does this notation mean: "0x7efff574 "\304\365\377~\300\365\377~", if you could point me somewhere in docs?


Solution

    1. A function can not return an array - it can only return a scalar or a struct or an union, or no value at all.

      But in addition to return value, it can have side effects. Here, as a side effect, it modifies the contents of the array whose first element is pointed to by s.

    2. function does not properly null-terminate the string, hence using it with printf leads to undefined behaviour. As a fix, add

      s[i] = 0;
      

      just before the return i;

    3. The notation is address of the pointer value 0x7efff574 in hexadecimal followed by the contents of the null-terminated string at that address, using the \ooo octal escapes for unprintable characters. In this case, the array contains some random garbage upon entering the function.


    P.S. Please indent your code properly and consistently. Spare the space bar and spoil the code.