Thread: pointers are confusing!

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    7

    pointers are confusing!

    Hi everybody.
    I'm new to coding C.I can already code in VB and ASM,but after spending a lot of time coding in VB its really hard for me to learn to code in C.Especially pointers are really confusing.I was wondering,if someone could please check the following code and tell me what i'm doing wrong with it:


    Code:
    #include <stdio.h>
    /*Declare record as a struct:
    */
    struct record
    {
    char name[100];
    int age;
    char key [25];
    };
    
    /*The following function is used for creating a record of 
    some names,their ages,and a key which would be used to refer to
    them.
    */
    struct record create(int n,struct record*field)
    {
    int x;
    int a;
    char na[100],k[25];
    /*X is the int which'll be used in the for loop.Na is a
    temporary varible that holds the value of the name
    entered and then passes it to the record.Well it tries to,
    because the compiler throws an error on the line where it
    tries to pass the value.K is a temp. variable to hold
    the key,and a is the temp. variable to hold the age.Hope it 
    makes sense!
    */
    for (x=0;x<=n;x++)
    {
    printf("Field # %d:\n",x);
    printf("Name: ");
    scanf("%s",na);
    field->name=na;
    /*The above line is where the program execution breaks
    when i try to compile the program.When i say the execution
    breaks,i mean that the line turns red with a small cross(x)
    to its left.
    */
    printf("Age: ");
    scanf("%d",a);
    field->age=a;
    printf("Key: ");
    scanf("%s",k);
    field->key=k;
    printf("Field # %d set up.\n\n",x);
    return *field;
    ++ *field;
    }
    
    printf("All fields set up.\n");
    printf("Total fields set up:%d",n);
    
    }
    void show(int n,,struct record field);
    /*The above line is used to declare the function
    show.The actual function is below main().If i dont
    declare a function before name and write down its code
    below main,then i get an error.Thats why i declared it here.
    I hope i'm not more confusing than pointers are :-D
    */
    main()
    {
    int n;
    char yn[3];
    printf("Enter the number of records to create: ");
    scanf("%d",&n);
    printf("OK.\n\n");
    struct record field[n];
    create(n,&struct record field);
    printf("\n\nWould you like to view the records you've created? ");
    scanf("%s",yn);
    if (yn=="yes")
    {
    show(n,struct record field);
    }
    else
    {
    printf("would you like to create some new records? ");
    scanf("%s",yn);
    if (yn=="yes")
    {
    main();
    }
    else
    {
    printf("END.");
    }
    }
    }
    /*The function below shows the records created.*/
    void show(int n,struct record field)
    {
    int x;
    for (x=1;x<=n;x++)
    {
    printf("Name:%s",field[x].name);
    printf("Age:%d",field[x].age);
    printf("Key:%s",field[x].key);
    }
    
    printf("End of all records.\n\n");
    }

    When i try compiling this code,i get the following error:

    33 C:\WINDOWS\Profiles\****\Desktop\cprojs\testproj\m .c
    ISO C++ forbids assignment of arrays

    along with a number of other errors.but this one is highlighted when the compilation breaks.

    If someone could please help me with it i'd be really grateful.thanks.
    @li.
    Last edited by Salem; 07-08-2004 at 12:02 AM. Reason: Changed the 'b' tag to 'code', but its still not indented - the OP can fix that

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Code:
    scanf("%s",k);
    field->key=k;
    you can't do this in C. change it to
    Code:
    scanf("%s",k);
    strcpy(field->key,k");
    strings are not a data type in C they are arrays of characters.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    There are a couple of things that are wrong with your code. The way you pass your structure to your function is wrong. You want to pass the structure itself. you main should be
    Code:
    int main(void){
    and you should
    Code:
    return EXIT_SUCCESS
    at the end of your code.
    Code:
    ++ *field;
    not only is this line wrong it will never get executed. It is after the return. Your prototype
    Code:
    void show(int n,,struct record field);
    has one to many commas.
    Code:
     struct record field[n]
    you cannot create an array with variable number of arguments. You would have to make a linked list. A more advanced topic. So for now just create a number that is bigger than you'd expect ex: struct record field[100];
    Code:
     for (x = 0; x <= n; x++)    {
    should be
    Code:
    for(x=0;,<n;x++)
    remember that arrays start at 0 and end at elements-1. You have to change your idea for your create function. To fill all the values you have to pass the memory address of the first element of your array of structures
    Code:
    create(n,&field[n]);
    then in your create function in your for loop you have to change all field->... to field[n]->...
    Be careful all your scanf("%s"... calles leave a \n in the buffer; better is to replace the scanf with fgets(. It is in the FAQ on this website. Once again strings aren't objects in C so you can't do
    Code:
      if (yn == "yes")
    change it to
    Code:
    if(!strcmp(yn,"yes"))/*if strings are equal*/
    Don't worry about the weird return value for now. I know that is alot if you have any question just let us know we will be sure to answer it. And ask us what you need help with first because their is alot wrong with your code. Don't worry it gets easier

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    While you're at it, use [code] tags!

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    ^^^ You never cease to amaze me with your humor

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    8

    Cool

    Quote Originally Posted by ali1
    Hi everybody.
    I'm new to coding C.I can already code in VB and ASM,but after spending a lot of time coding in VB its really hard for me to learn to code in C.Especially pointers are really confusing.I was wondering,if someone could please check the following code and tell me what i'm doing wrong with it:


    Code:
    #include <stdio.h>
    /*Declare record as a struct:
    */
    struct record
    {
    char name[100];
    int age;
    char key [25];
    };
    
    /*The following function is used for creating a record of 
    some names,their ages,and a key which would be used to refer to
    them.
    */
    struct record create(int n,struct record*field)
    {
    int x;
    int a;
    char na[100],k[25];
    /*X is the int which'll be used in the for loop.Na is a
    temporary varible that holds the value of the name
    entered and then passes it to the record.Well it tries to,
    because the compiler throws an error on the line where it
    tries to pass the value.K is a temp. variable to hold
    the key,and a is the temp. variable to hold the age.Hope it 
    makes sense!
    */
    for (x=0;x<=n;x++)
    {
    printf("Field # %d:\n",x);
    printf("Name: ");
    scanf("%s",na);
    field->name=na;
    /*The above line is where the program execution breaks
    when i try to compile the program.When i say the execution
    breaks,i mean that the line turns red with a small cross(x)
    to its left.
    */
    printf("Age: ");
    scanf("%d",a);
    field->age=a;
    printf("Key: ");
    scanf("%s",k);
    field->key=k;
    printf("Field # %d set up.\n\n",x);
    return *field;
    ++ *field;
    }
    
    printf("All fields set up.\n");
    printf("Total fields set up:%d",n);
    
    }
    void show(int n,,struct record field);
    /*The above line is used to declare the function
    show.The actual function is below main().If i dont
    declare a function before name and write down its code
    below main,then i get an error.Thats why i declared it here.
    I hope i'm not more confusing than pointers are :-D
    */
    main()
    {
    int n;
    char yn[3];
    printf("Enter the number of records to create: ");
    scanf("%d",&n);
    printf("OK.\n\n");
    struct record field[n];
    create(n,&struct record field);
    printf("\n\nWould you like to view the records you've created? ");
    scanf("%s",yn);
    if (yn=="yes")
    {
    show(n,struct record field);
    }
    else
    {
    printf("would you like to create some new records? ");
    scanf("%s",yn);
    if (yn=="yes")
    {
    main();
    }
    else
    {
    printf("END.");
    }
    }
    }
    /*The function below shows the records created.*/
    void show(int n,struct record field)
    {
    int x;
    for (x=1;x<=n;x++)
    {
    printf("Name:%s",field[x].name);
    printf("Age:%d",field[x].age);
    printf("Key:%s",field[x].key);
    }
    
    printf("End of all records.\n\n");
    }

    When i try compiling this code,i get the following error:

    33 C:\WINDOWS\Profiles\****\Desktop\cprojs\testproj\m .c
    ISO C++ forbids assignment of arrays

    along with a number of other errors.but this one is highlighted when the compilation breaks.

    If someone could please help me with it i'd be really grateful.thanks.
    @li.
    yes i agree with u , that pointers in "C" r a bit confusing .

    The main thing to understand in pointers is that the difference between * and & .
    '*' means the value at address.
    '&' means the address of variable.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by ali1
    Hi everybody.
    I'm new to coding C.I can already code in VB and ASM,but after spending a lot of time coding in VB its really hard for me to learn to code in C

    At http://www.cs.virginia.edu/~evans/cs...gs/ewd498.html

    we see, in an article from Sigplan Notices Volume 17 No. 5, How do We Tell Truths that Might Hurt

    Quote Originally Posted by Edsgar W. Dijsktra
    It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration.
    (Someone said this article was tongue-in-cheek, but someone else, I forget who, said, "Many a truth has been spoken in jest.")

    It's not impossible; lots of people have made the transition, but it's not always easy. Keep the faith.

    Regards,

    Dave

    (p.s. No, I'm not the "evans" in the url. You don't really think my name is Dave Evans, do you?)
    Last edited by Dave Evans; 09-07-2004 at 03:27 PM.

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    My first programming language was GWBasic and I used it to death. So I'm living proof that the transition can be made with only minor psychological damage
    If you understand what you're doing, you're not learning anything.

  9. #9
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by linuxdude
    There are a couple of things that are wrong with your code. The way you pass your structure to your function is wrong. You want to pass the structure itself. you main should be
    Code:
    int main(void){
    and you should
    Code:
    return EXIT_SUCCESS
    at the end of your code.
    And don't forget that if you do use the EXIT_SUCCESS macro, as linuxdude suggested, be sure to #include <stdlib.h>

    ~/

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by itsme86
    My first programming language was GWBasic and I used it to death. So I'm living proof that the transition can be made with only minor psychological damage
    I started with BASIC on a C=64, later AmigaBASIC, GWBasic (which was pretty bad compared to the Amiga), QBasic, QuickBASIC (which is MS's QB compiler).
    I then moved on to Pascal and later Delphi, on to C, Java and this dreaded thing called Scheme (for the uni), x86 Assembler and C#.

    I wouldn't suggest the use of BASIC now either, though I'm not familiar with those modern dialects like VB. But then again, I don't believe it has a negative impact on future programmers either. Today I'd say, everything but asm, C and C# can be ignored. Those 3 provide all that is needed for low and high level programming. Java is nice, but in C# you can really see where it comes from and that a lot of Javas faults haven't been made again. They also borrowed heavily from Delphi. Not a bad thing to do.

    And now I've gone completely offtopic. Maybe BASIC really left some damage...

  11. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    Dear God man, use some indentations.

    Before learning pointers, you need to learn to make your code readable.

    Include whitespace (press enter) between statements, or in order to separate blocks of code.

    Kind of like:

    Code:
    #include <stdio.h>
    
    int main ()
    
    {
    
         printf("Arrr matey.");
         return 0;
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Variable pointers and function pointers
    By Luciferek in forum C++ Programming
    Replies: 11
    Last Post: 08-02-2008, 02:04 AM
  3. Dynamic array of pointers
    By csisz3r in forum C Programming
    Replies: 8
    Last Post: 09-25-2005, 02:06 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. moving pointers to pointers
    By Benzakhar in forum C++ Programming
    Replies: 9
    Last Post: 12-27-2003, 08:30 AM