LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-13-2005, 09:14 AM   #1
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Rep: Reputation: 15
What is the problem with this short code?


Can someone please tell what im doing wrong?

Here is the code:

Code:
#include <stdio.h>
#include <stdlib.h>

#define myArray "sc"
#define myChar 'c'

int main(int c, char** argv)
{
   char myCharArray [ ] = myArray;
   
   printf("%s\n", myCharArray);
   char myArrayC [ ] = xo ();
   printf("%s\n", myArrayC);
   
   return 0;
}

char* xo ()
{
   char moo [ ] = "scd";
   char* xo = moo;
   return xo;
}
And here are the errors i get:

Code:
linux3[32]% gcc -ansi test.c
test.c: In function `main':
test.c:12: error: invalid initializer
test.c: At top level:
test.c:19: error: conflicting types for 'xo'
test.c:12: error: previous implicit declaration of 'xo' was here
Thanks, This language is C by the way

Mistro116
 
Old 11-13-2005, 09:34 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

This might be better:
Code:
#include <stdio.h>
#include <stdlib.h>

#define MYSTRING "sc"
#define MYCHAR 'c'

char* xo ();   /* Making a prototype optional ... but good form */

int 
main(int c, char **argv)
{
   char myCharArray [ ] = MYSTRING; /*Use preprocessor macro "sc" */
   char myOtherCharArrayC[10];  /* Allocate space only - still uninitialized */

   /* myCharArray statically initialized: let's use it */
   printf("%s\n", myCharArray);  /*OK */

   /* myOtherCharArrayC must be initialized */
   strncpy (myOtherCharArrayC,  xo (), sizeof myOtherCharArrayC);
   printf("%s\n", myOtherCharArrayC); /* OK */
   
   /* We can safely change myOtherCharArrayC */
   myOtherCharArrayC[0] = MYCHAR;
   printf("%s\n", myOtherCharArrayC); /* OK */

   /* We *should* not be changing myCharArray: this is arguably a bug:
      Even if it doesn't crash (and it won't), it's still *wrong*! */
   myCharArray[0] = MYCHAR;   /* BAD! */
   printf("%s\n", myCharArray);

   return 0;
}

char* 
xo ()
{
   static char moo [ ] = "scd";  /* Must be static, or a bug! */
   return moo;
}

Last edited by paulsm4; 11-13-2005 at 09:52 AM.
 
Old 11-13-2005, 09:48 AM   #3
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Original Poster
Rep: Reputation: 15
Code:
linux3[36]% gcc -ansi test.c
test.c: In function `main':
test.c:10: error: parse error before '/' token
test.c:16: error: `myOtherCharArrayC' undeclared (first use in this function)
test.c:16: error: (Each undeclared identifier is reported only once
test.c:16: error: for each function it appears in.)
test.c: In function `xo':
test.c:24: error: parse error before '/' token
Still getting these errors when i compile the code you posted.
 
Old 11-13-2005, 09:54 AM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
I was using C++ ("//") comments. See the above changes with "/* */" comments, andl - hopefully useful - notes.

Your .. PSM
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
small syntax problem with C code (implemented in Code Composer Studio) illiniguy3043 Programming 6 01-07-2008 02:14 AM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM
C Code problem ar1 Programming 3 05-09-2005 07:04 AM
What is the problem with the below C++ code? coolguy_iiit Programming 4 01-24-2005 12:28 PM
short filename problem with solaris Gary_B9 Solaris / OpenSolaris 1 01-09-2003 01:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration