erros

This is a discussion on erros within the C Programming forums, part of the General Programming Boards category; i keep getting an error in this prog.. #include <stdio.h> #define xlordt '*' struct char xlordt[7]={ {x,l,o,r,d,t}, }; int main() ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    227

    erros

    i keep getting an error in this prog..

    #include <stdio.h>

    #define xlordt '*'

    struct char xlordt[7]={
    {x,l,o,r,d,t},
    };

    int main()
    {

    int x;

    for (x=0; x<1; x++)
    putc( xlordt[x], stdout);

    return 0;
    }


    the error that i keep getting is..
    dd.c:5: parse error before `char'
    dd.c: In function `main':
    dd.c:15: subscripted value is neither array nor pointer

    can anyone tell me what this means... (running under linux)

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Comments:

    1. struct char is not valid
    2. you use xlordt two times
    3. the array initialisation is wrong

    Explanation:

    >dd.c:5: parse error before `char'

    That's because of point 1.

    >dd.c: In function `main':
    >dd.c:15: subscripted value is neither array nor pointer

    That's because of point 2.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    umm sorry but. i really dont get what you are saing(newbie) =(

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    You defined xlordt as '*'. This would mean that

    struct char xlordt[7]={

    would become

    struct char '*'[7]={

    which is not valid.

    The construction "struct char" is invalid. The keyword "struct" means that you're defining a structure, like in:

    struct mystruct
    {
    /* some data */
    };

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    87
    This is quite a crap way of doing it, but hey it works.

    Code:
    #include <stdio.h>
    
    typedef struct xlordt{
      char xlord[7];
    }xlord_t;
    
    int main(){
      int i;
      xlord_t test;
      test.xlord[0] = 'x';
      test.xlord[1] = 'l';
      test.xlord[2] = 'o';
      test.xlord[3] = 'r';
      test.xlord[4] = 'd';
      test.xlord[5] = 't';
      for(i = 0; i<sizeof(test.xlord); i++)
        printf("%c",test.xlord[i]);
      return 0;
    }
    PuterPaul.co.uk - Portfolio site

  6. #6
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    struct char xlordt[7]={
    {x,l,o,r,d,t},
    };
    when your defining a structure your defining a type you cant initialise a type
    Code:
    typedef struct{
        char name[7];
    }XLORDT;
    now you declare a variable of type XLORDT
    Code:
    XLORDT xlordt;
    when you refer to a field of your structure you need to use the dot operator
    Code:
    putc( xlordt.name[x], stdout);
    However if you only have 1 field in your structure it's all a waste of time, you may as well just have an array.
    Code:
    #include <stdio.h> 
    #include <string.h>
    
    typedef struct{ 
    	char name[7];
    }XLORDT; 
    
    int main( void ) 
    { 
    
    	int x;
    	XLORDT xlordt;
    
    	strcpy( xlordt.name, "xlordt" );
    
    	for (x=0; x<7; x++) 
    	putc( xlordt.name[x], stdout); 
    
    	return 0; 
    }
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    mind if i ask what does.. typedef means i know i read it somewere but i didnt understand it to much. btw thanx

  8. #8
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    To xlordt,

    Note that typedef is used to define a type, like in:

    typedef unsigned int UINT;

    Here there is a type MyUINT created. This type is the same as unsigned int. It is usually used to prevent from typing very much. Instead of

    unsigned int var;

    one can now use

    UINT var;

    Code:
    #include <stdio.h> 
    
    char xlordt [7] = {'x','l','o','r','d','t','\0'};
    
    int main (void) 
    {
    	int x;
    	
    	printf ("%s\n", xlordt);
    	
    	for (x = 0; x < 7; x++)
    	    printf ("%c", xlordt [x]);    
    
    	return 0; 
    }
    Last edited by Shiro; 04-13-2002 at 08:21 AM.

  9. #9
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    btw, xlordt you have posted loads of times, it's about time you started using code tags
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  10. #10
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    thanx.. sorry sometime i dont understand something... thanx

  11. #11
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    There's no need for excuse. Everybody sometimes doesn't understand something and needs some help, that's why these boards are here.

  12. #12
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    its imbarassing that i have been trying to code for quite some time now and i have not learn well enough. There are lots of thing that i really dont understand. Other things i learnd well but c programming is a great big challenge for, but anyways i guess i will keep on tring. btw thanx again for your help..

  13. #13
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    keep trying and you will succeed.
    The important thing is that you took up the challenge, unlike so many who talk about it but never do it.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  14. #14
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    heh ya easyer said then done.. the code that i posted here im tring to change it to pring in to ascii and i dont want to post it till i try, but i have the leest bit on how to do it heheh.. but thanx for your help on keeping my head up..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lib erros when trying to compile C(++) in Kdevelop
    By Maragato in forum Tech Board
    Replies: 1
    Last Post: 07-29-2005, 03:01 PM
  2. qUEstion about erros on compiling
    By cogeek in forum C Programming
    Replies: 9
    Last Post: 07-30-2004, 01:27 PM
  3. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 05:17 AM
  4. erros 2
    By prlove71 in forum C++ Programming
    Replies: 3
    Last Post: 03-13-2002, 04:36 PM
  5. erros
    By prlove71 in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2002, 04:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21