Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

please explain the below c program

Status
Not open for further replies.

rangerskm

Full Member level 4
Full Member level 4
Joined
Jan 23, 2013
Messages
199
Helped
0
Reputation
2
Reaction score
0
Trophy points
1,296
Activity points
2,663
1) this program helps to study the concept of functions.i am not clear in function concepts in c.please point out some examples in functions
2)i tried to compile the below code but i didnt understand the logic behind it .these may contain mistakes .please find them and try to help me in this
3)this program is taken from K AND R c reference book.
Code:
#include <stdio.h>
   #define MAXLINE 1000 /* maximum input line length */

   int getline(char line[], int max)
   int strindex(char source[], char searchfor[]);

   char pattern[] = "ould";   /* pattern to search for */

   /* find all lines matching pattern */
   main()
   {
       char line[MAXLINE];
       int found = 0;

       while (getline(line, MAXLINE) > 0)
           if (strindex(line, pattern) >= 0) {
               printf("%s", line);
               found++;
           }
       return found;
   }

   /* getline:  get line into s, return length */
   int getline(char s[], int lim)
   {
       int c, i;

       i = 0;
       while (--lim > 0 && (c=getchar()) != EOF && c != '\n')
           s[i++] = c;
       if (c == '\n')
           s[i++] = c;
       s[i] = '\0';
       return i;
   }

   /* strindex:  return index of t in s, -1 if none */
   int strindex(char s[], char t[])
   {
       int i, j, k;

       for (i = 0; s[i] != '\0'; i++) {
           for (j=i, k=0; t[k]!='\0' && s[j]==t[k]; j++, k++)
               ;
           if (k > 0 && t[k] == '\0')
               return i;
       }
       return -1;
   }
 

You need to elaborate on the specifics of the generated error messages.

Please post the generated error messages, the specific compiler utilized and the OS.

The following are function prototypes:

Code:
   int getline(char line[], int max);
   int strindex(char source[], char searchfor[]);

Which provide a forward declaration of the actual function definitions below:

Code:
           /* getline:  get line into s, return length */
           int getline(char s[], int lim)
           {
               int c, i;

               i = 0;
               while (--lim > 0 && (c=getchar()) != EOF && c != '\n')
                   s[i++] = c;
               if (c == '\n')
                   s[i++] = c;
               s[i] = '\0';
               return i;
           }

           /* strindex:  return index of t in s, -1 if none */
           int strindex(char s[], char t[])
           {
               int i, j, k;

               for (i = 0; s[i] != '\0'; i++) {
                   for (j=i, k=0; t[k]!='\0' && s[j]==t[k]; j++, k++)
                       ;
                   if (k > 0 && t[k] == '\0')
                       return i;
               }
               return -1;
           }

One fact you must learn/understand is the concept between a function declaration/prototype and a function definition.

The function declaration/prototype is simply a place holder to alert the compiler of a functions existence, while a function definition contains the actual code of the function.

As the actual parsing and compilation of the code occurs from the top of the source code file down, the purpose of the function prototypes is to alert the compiler of the existence of any functions utilized in the code which are later defined further down within the source code file.

An alternative would be to place the actual definitions of the functions in the source code file before the main() routine in place of the function prototypes.

In circumstances where the function definitions are contained within another file, the function protoypes are often provided in a header file which is included at the top of any source code file which utilizes these functions.

An example of this arrangement would be the following header file:

Code:
#include <stdio.h>

The actual function definitions are contained in another source code file, typically named stdio.c, which is compiled and then linked with the source code file containing main().

BigDog
 

compiler used -gcc
os -fedora 19
error message
[sathish@localhost ~]$ gcc funckr.c
funckr.c: In function ‘gettline’:
funckr.c:7:4: error: parameter ‘pattern’ is initialized
char pattern[] = "ould"; /* pattern to search for */
^
funckr.c:10:4: error: expected declaration specifiers before ‘main’
main()
^
funckr.c:26:4: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
funckr.c:42:4: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
funckr.c:4:8: error: old-style parameter declarations in prototyped function definition
int gettline(char line[], int max)
^
funckr.c:52:4: error: expected ‘{’ at end of input
}
 

At first glance, one issue is the missing semicolon after the first declaration:

Code:
   int getline(char line[], int max)[COLOR="#FF0000"];[/COLOR]
   int strindex(char source[], char searchfor[]);

Correct this syntax error and then recompile. Post any additional errors.

BigDog
 

follow the changes I didnt debug the program just errors corrected

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
   #define MAXLINE 1000 /* maximum input line length */
 
   int getline(char*, int);
   int strindex(char*, char*);
 
   char pattern[] = "ould";   /* pattern to search for */
 
   /* find all lines matching pattern */
   int main()
   {
       char line[MAXLINE];
       int found = 0;
 
       while (getline(line, MAXLINE) > 0)
             if (strindex(line, pattern) >= 0) {
               printf("%s", line);
               found++;
       }
       printf("%d",found);
   }
 
   /* getline:  get line into s, return length */
   int getline(char s[], int lim)
   {
       int c, i;
 
       i = 0;
       do{
        
        c = getche();
        
        s[i++] = c;
        
       } while (--lim > 0 && c != EOF && c != '\r');
 
       s[i] = '\0';
 
       return i;
   }
 
   /* strindex:  return index of t in s, -1 if none */
   int strindex(char s[], char t[])
   {
       int i, j, k;
 
       for (i = 0; s[i] != '\0'; i++) {
           for (j=i, k=0; t[k]!='\0' && s[j]==t[k]; j++, k++)
               ;
           if (k > 0 && t[k] == '\0')
               return i;
       }
       return -1;
   }

 

getline is a pre-defined C function declared in the stdio.h header file. Change your function name to something else and then it will compile without any errors.
 

getline is a pre-defined C function declared in the stdio.h header file. Change your function name to something else and then it will compile without any errors.
Is there a getline function, I think it was gets() in stdio.h.. The above program compiles in the dev c++...
 

I'm compiling in ubuntu using Gcc.

This is the declaration of the function in stdio.h, also its available in the man page.
Code:
extern _IO_ssize_t getline (char **__restrict __lineptr,
                            size_t *__restrict __n,
                            FILE *__restrict __stream) __wur;
 

how to declare the above getline function .please explain the function with an example..
 

2) please explain the below program .and also give me links for clear explanation about pointers,pointers in array ,pointers in strings and data structures...
Code:
#include<stdio.h>
void main()
{
int *j,i=4;
j=i*2;
printf("j=%d\n",j);
}
error message when executing...
[sathish@localhost ~]$ gcc pointest15.c
pointest15.c: In function ‘main’:
pointest15.c:5:2: warning: assignment makes pointer from integer without a cast [enabled by default]
j=i*2;

- - - Updated - - -

3) explain the below program

Code:
#include<stdio.h>
float a=3.14;
float **z;
float **y;
float ***x;
float ****v;
float ****w;
float **fun1(float*);
float ****fun2(float***);
void main()
{
z=fun1(&a);
printf("%u %f\n",z,**z);
}
float **fun1(float *z)
{
y=&z;
v=fun2(&y);
return (**v);
}
float ****fun2(float ***x)
{
w=&x;
return (w);
}
 

2) please explain the below program .and also give me links for clear explanation about pointers,pointers in array ,pointers in strings and data structures...
Code:
#include<stdio.h>
void main()
{
int *j,i=4;
j=i*2;
printf("j=%d\n",j);
}

It was very easy you are assigning a integer to a pointer variable

run the following program


Code C - [expand]
1
2
3
4
5
6
7
8
#include<stdio.h>
void main()
{
int *j, i = 4, x;
j = &x;    // A pointer shd be initialized before used
*j= i*2;  // you cant assign a integer variable to a pointer
printf(" j = %x\n *j = %d\n", j , *j);
}



- - - Updated - - -

I think any ordinary human can not think about your second program.....
 
2) please explain the below program .and also give me links for clear explanation about pointers,pointers in array ,pointers in strings and data structures...
Code:
#include<stdio.h>
void main()
{
int *j,i=4;
j=i*2;
printf("j=%d\n",j);
}
error message when executing...


- - - Updated - - -

3) explain the below program

Code:
#include<stdio.h>
float a=3.14;
float **z;
float **y;
float ***x;
float ****v;
float ****w;
float **fun1(float*);
float ****fun2(float***);
void main()
{
z=fun1(&a);
printf("%u %f\n",z,**z);
}
float **fun1(float *z)
{
y=&z;
v=fun2(&y);
return (**v);
}
float ****fun2(float ***x)
{
w=&x;
return (w);
}

for the first one
Code:
#include<stdio.h>
void main()
{
int *j,i=4;
j= &i;
*j = *j*2;
printf("j=%d\n",*j);
}

for the 2nd program refer https://bytes.com/topic/c/answers/663582-pointer-pointing-itself-giving-different-value
In this you just used pointer that points by itself...

for poiter tutorial in C you can refer https://www.tutorialspoint.com/cprogramming/c_pointers.htm
 
sorry to disturb you friends..i am having so much difficult programs in pointers to understand,please explain me these programs..try this one and explain
Code:
#include<stdio.h>
void display(int *,int,int);
void show(int (*q)[4],int,int);
void print(int q[][4],int,int);
void main()
{
int a[3][4]={1,2,3,4,5,6,7,8,9,0,1,6};
display(a,3,4);
show(a,3,4);
print(a,3,4);
}
void display(int *q,int row,int col)

{
int i,j;
for(i=0;i<row;i++)
{
 for(j=0;j<col;j++)
printf("%d",*(q+i*col+j));
printf("\n");
}
printf("\n");
}
void show(int (*q)[4],int row,int col)
{
int i,j;
int *p;
for(i=0;i<row;i++)
{
 p=q+i;
 for(j=0;j<col;j++)
printf("%d",*(p+j));
printf("\n");
}
printf("\n");
}
void print (int q[][4],int row,int col)
{
int i,j;
for(i=0;i<row;i++)
{
 for(j=0;j<col;j++)
printf("%d",q[i][j]);
printf("\n");
}
printf("\n");

}
i am compiled using gcc compiler in fedora

the error i got as
parrayy25.c: In function ‘main’:
parrayy25.c:8:1: warning: passing argument 1 of ‘display’ from incompatible pointer type [enabled by default]
display(a,3,4);
^
parrayy25.c:2:6: note: expected ‘int *’ but argument is of type ‘int (*)[4]’
void display(int *,int,int);
^
parrayy25.c: In function ‘show’:
parrayy25.c:30:3: warning: assignment from incompatible pointer type [enabled by default]
p=q+i;
 

change

Code C - [expand]
1
display(a,3,4);


to

Code C - [expand]
1
display(&a,3,4);



change

Code C - [expand]
1
void show(int (*q)[4],int row,int col)


to

Code C - [expand]
1
void show(int *q[4],int row,int col)


change

Code C - [expand]
1
p=q+i;


to

Code C - [expand]
1
*p=q+i;

 
Type casting is the problem in two places...


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<stdio.h>
void display(int *,int,int);
void show(int (*q)[4],int,int);
void print(int q[][4],int,int);
void main()
{
int a[3][4]={1,2,3,4,5,6,7,8,9,0,1,6};
display((int*)a,3,4);
show(a,3,4);
print(a,3,4);
getch();
}
void display(int *q,int row,int col)
 
{
int i,j;
for(i=0;i<row;i++)
{
 for(j=0;j<col;j++)
printf("%d",*(q+i*col+j));
printf("\n");
}
printf("\n");
}
void show(int (*q)[4],int row,int col)
{
int i,j;
int *p;
for(i=0;i<row;i++)
{
 p=(int*)q+i;
 for(j=0;j<col;j++)
printf("%d",*(p+j));
printf("\n");
}
printf("\n");
}
void print (int q[][4],int row,int col)
{
int i,j;
for(i=0;i<row;i++)
{
 for(j=0;j<col;j++)
printf("%d",q[i][j]);
printf("\n");
}
printf("\n");
 
}

 

Attachments

  • adc.exe
    235 KB · Views: 128
thank you very much venkatesh and jeyanth brother .i got the output when i removed the getch() statement from main.whats the reason ??
2) then why we converting the type cast here as q is already taken as int in void show called function .??
 
Last edited:

I added the getch to hole the output screen that is not needed in the program,

when you are adding a pointer q with inter i, it will be form a new integer value but you need a pointer value for p, so that


Code C - [expand]
1
p=(int*)q+i;



it works..
 
can you explain with a small example

- - - Updated - - -

5th program)
please check this one
Code:
#include<stdio.h>
# define ROW 3
#define COL 4

void main()
{
int i,j;
int (*b)[COL];
int *p;
b=fun2();
printf("array of b[][] in main\n");
for (i=0;i<ROW;i++)
{
 p=(int*)b+i;
 for (j=0;j<COL;j++)
{
printf("fun a%d",*(p+j));
}
printf("\n");
}
}
int (*fun2())[COL]
{
static int b[ROW][COL]={1,2,3,4,5,6,7,8,9,0,1,6};
int i,j;
printf("array a[][] in fun\n");
for(i=0;i<ROW;i++)
{
for(j=0;j<COL;j++)
printf("%d",b[i][j]);
printf("\n");
}
return b;
}
this is the error message i got
this program helps to learn returning array from functions
[sathish@localhost ~]$ gcc parray28.2.c
parray28.2.c: In function ‘main’:
parray28.2.c:10:2: warning: assignment makes pointer from integer without a cast [enabled by default]
b=fun2();
^
parray28.2.c: At top level:
parray28.2.c:22:7: error: conflicting types for ‘fun2’
int (*fun2())[COL]
^
parray28.2.c:10:3: note: previous implicit declaration of ‘fun2’ was here
b=fun2();
 

nice, you are interested in programming but by trying to execute existing programs will not trigger you much so think and write you own programs and if you got stuck any where then you will know some new concepts....
 

you are right brother,i am new to pointers,so i am not able to grasp the full concepts in it.i am referring the book 'understanding pointers in C'.i am executing those programs to understand the concept.these programs makes me trouble always .thats why i am seeking help from you.i have to learn these topics quickly.if possible guide me in this.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top