Solved

Compilation error

Posted on 2003-12-04
11
525 Views
Last Modified: 2010-04-15
1. The below code is OK if compiled in ANSI C Compiler but not on normal C Compiler. What's the solution for this problem?

char buf[100];

if ((numbytes=recv(new_fd, buf, 100-1, 0)) == -1) {
   perror("recv");
  exit(1);
}

buf[numbytes] = '\0';
printf("Received: %s\n",buf);

char *search_str = buf; ==> error at this line
--------------------------------------------------
error 1000: Unexpected symbol: "char".
error 1588: "search_str" undefined.
--------------------------------------------------

printf("search_str == %s\n", search_str); ==> error at this line
---------------------------------------------------
error 1549: Modifiable lvalue required for assignment operator.
---------------------------------------------------

2. Can anybody explain to me or suggest to me for any good interactive tutorial on pointer?
0
Comment
Question by:showbix
  • 5
  • 5
11 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9873531
Hi showbix,

I presume you meant not compiling in ANSI C but compiling in "normal" C

1. in C all declarations must precede the first statement...
To be more specific declarations are allowed only at the beginning of a "block"

2. you cannot assign strings like a=b ... nor can you compare them like a==b ... you have to use strcpy and strcmp respectively

http://www.cs.cf.ac.uk/Dave/C/CE.html
is a good tutorial

Cheers!
Sunny:o)
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9873562
just noticed that you were not assigning strings there ... just  pointer manipulation ... that will work fine once search_str is declared properly ...

move char * search_str to the beginning of the function and everything should work well.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9874047
>> 1. in C all declarations must precede the first statement...

But Sunny, that was old.... the latest accepted standard C99 overrides it. It is now legal in C to declare variables anywhere in a program, in any block. However, all compilers don't support it as yet.
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 45

Expert Comment

by:sunnycoder
ID: 9874075
> It is now legal in C to declare variables anywhere in a program, in any block.
Nope mayank ... Variables can be declared only at the *beginning* of a block
C++ permits you to declare variables anywhere in the program but C still requires declarations to be at the beginning of a block
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9879830
Ok, sorry I got confused with program and block :-), so I thought that you were wrong.

Anyways, Sunny, pls comment on: http://www.experts-exchange.com/Programming/Q_20816080.html
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9880190
sorry mayank ... I do not have much idea about java or .net
I am still limited to C and linux
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9880300
ok, no worries :-)
0
 

Author Comment

by:showbix
ID: 9880797
Hey guys,
Let me explain what I need to achieve here.

I read the data using recv function and insert it into buf variable
---------------------------------------------------------------------
char buf[100];


if ((numbytes=recv(new_fd, buf, 100-1, 0)) == -1) {
   perror("recv");
   exit(1);
}

buf[numbytes] = '\0';
printf("Received: %s\n",buf);

char *search_str = buf;

if (strstr(search_str,"SUCCESS") != NULL)
{
  printf("SUCCESS found\n");
}
  else
{
  printf("SUCCESS not found\n");
}

---------------------------------------------------------------------
I tried to copy the buf value into search_str so that I can perform the sub-string search.
From search_str variable I would like to scan a sub-string (e.g SUCCESS).

The code above works fine in ANSI C compiler but it throw me erros when using normal C Compiler.
The error is at this line --- char *search_str = buf;

Putting the declaration "char *search_str = buf;" at the beginning of function will need me to put another statement(which I dont know) to copy the buf value into the search_str variable.
 

How do I rectify it so that the program works?

FYI, I'm in the UNIX environment.




0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 25 total points
ID: 9880914
char buf[100] ;
char * search_str = NULL ;

if ((numbytes=recv(new_fd, buf, 100-1, 0)) == -1) {
   perror("recv");
   exit(1);
}

buf[numbytes] = '\0';
printf("Received: %s\n",buf);

search_str = buff ;
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 25 total points
ID: 9880916
>Putting the declaration "char *search_str = buf;" at the beginning of function will need me to put another statement(which I
>dont know) to copy the buf value into the search_str variable.
just move the declaration to the beginning and let the assignment remain as it is


char buf[100];
char * search_str; //declaration here

if ((numbytes=recv(new_fd, buf, 100-1, 0)) == -1) {
  perror("recv");
  exit(1);
}

buf[numbytes] = '\0';
printf("Received: %s\n",buf);

search_str = buf;     //assignment remains

if (strstr(search_str,"SUCCESS") != NULL)
{
 printf("SUCCESS found\n");
}
 else
{
 printf("SUCCESS not found\n");
}
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9880917
On second thought:

char buf[100] ;
char * search_str = buf ;

if ((numbytes=recv(new_fd, buf, 100-1, 0)) == -1) {
   perror("recv");
   exit(1);
}

buf[numbytes] = '\0';
printf("Received: %s\n",buf);

// because 'buf' will always have the same base address,so it can be assigned to search_str right at the start, after declaration.
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mixing C++ and C code elegantly 10 158
distributed computing in Python 4 133
convert char array to number in c 5 86
Using popen() and gunzip() to open file in HTTPServer 6 65
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question