Thread: pointers, structures, and strings

  1. #1
    Registered User
    Join Date
    Nov 2022
    Posts
    3

    pointers, structures, and strings

    Code:
    // This is just a basic description (representation) of what I'm trying
    // to do and what is happening.  I haven't programmed in C in a 
    // long time and I'm trying to refresh my skills.  I understand that 
    // things change and memory fails.  I'm using gcc 11.3 on a win10
    // machine.  This file should be fully compliable.
    
    
    #include <stdio.h>
    #include <string.h>
    
    
    struct x {
    char *y;
    };
    
    #define MAX 100
    struct x *z[MAX], *zz;
    
    
    int main ()
    {
    char a[100];
    int n=15;
    
    
    // I'm using fgetc to read one char at a time into a[] from a datafile
    // and parse as I go.
    
    
    // My "sample" input:
    a[0] = 'X'; a[1] = 'X'; a[2] = 'X'; a[3] = '\0';
    printf("a = %s\n",a);
    
    
    // So I use *zz to build the "local record" and this is where my
    // problem is:
    
    
    // Three main versions to do the same thing?? (only using one at
    // a time):
    //zz->y = &a[0];
    //*zz->y = a[0]; *(++zz->y) = a[1]; *(++zz->y) = a[2]; *(++zz->y) = a[3]; 
    strcpy(zz->y,&a[0]);
    
    
    printf("zz->y = %s\n",zz->y);  // This doesn't print because I believe
                                                 // the previous statements cause a
                                                 // segmentation fault and the run
                                                 // terminates.
    
    
    // When done with building the local record, I want to assign it to 
    // the next location in *z[]:
    z[n] = zz;
    printf("z[%i]->y = %s\n",n,z[n]->y);
    
    
    //I would also like to zero out *zz and a[] after every iteration.
    }
    // Where am I being slopy?  What changes are needed to make this
    // work?  I would prefer using strcpy.
    // -- Ravenhawk
    Last edited by Ravenhawk; 11-07-2022 at 04:06 PM.

  2. #2
    Registered User
    Join Date
    Apr 2021
    Posts
    103
    You declare zz as a struct pointer, but don't ever initialize it.

    I suggest you just declare a new instance of the struct inside your function or inside your loop, and use that as your zz value.

  3. #3
    Registered User
    Join Date
    Nov 2022
    Posts
    3
    That appears to have worked - thank you.

  4. #4
    Registered User
    Join Date
    Nov 2022
    Posts
    3
    Code:
    /*
    The previous code and solution has evolved into a different issue now.  When this program is run, 
    it produces this output:
    zzz.y = Hello <> zzz.y2 = (null)
    zzz.y = World - zzz.y2 = World
    z[15]->y = World ->y2 = World
    
    When I add the 'a1' variable, in place of the 2nd instance of the 'a' variable, I get this output:
    zzz.y = Hello <> zzz.y2 = (null)
    zzz.y = Hello - zzz.y2 = World
    z[15]->y = Hello ->y2 = World
    
    This is a pointer issue that I can't really comphrend.  I see what is happening, but I don't understand
    it.  The 2nd iteration is what I am looking for but I do not want to have to use a different string 
    variable for each new 'word'.  I thought that strcpy made a new copy of the source, pointed to by 
    only destination, but it appears that destination is still pointing to source (not just a copy).  
    Therefore, when 'a1' gets a new value in the second instance, .y points to it too and not the first word.
    
    How can this be fixed?
    
    */
    #include <stdio.h>
    #include <string.h>
    
    struct x {
    char *y;
    char *y2;
    };
    
    #define MAX 100
    struct x *z[MAX], zzz;
    
    int main ()
    {
    char a[100];
    //char a1[100];
    int n=15;
    
    strcpy(a,"Hello\0");
    zzz.y = &a[0]; 
    printf("zzz.y = %s <> zzz.y2 = %s\n",zzz.y,zzz.y2);
    
    strcpy(a,"World\0"); 
    zzz.y2 = &a[0]; 
    //strcpy(a1,"World\0"); 
    //zzz.y2 = &a1[0]; 
    printf("zzz.y = %s - zzz.y2 = %s\n",zzz.y,zzz.y2);  
    
    z[n] = &zzz;
    printf("z[%i]->y = %s ->y2 = %s\n",n,z[n]->y,z[n]->y2);
    
    
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,436
    What are you trying to achieve?

    This, without the need to specify an array size for the struct members?
    Code:
    struct x {
        char y[10];
        char y2[20];
    };
     
    #define MAX 100
    struct x *z[MAX];
    > strcpy(a,"Hello\0");
    There's no need to put a \0 on the end of strings, you get one for free anyway.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 04-02-2012, 07:38 PM
  2. C Strings in Structures.
    By SlyMaelstrom in forum C++ Programming
    Replies: 15
    Last Post: 10-12-2005, 04:29 PM
  3. Pointers to Classes || pointers to structures
    By C++Child in forum C++ Programming
    Replies: 24
    Last Post: 07-30-2004, 06:14 PM
  4. Strings and Structures
    By Kinasz in forum C Programming
    Replies: 3
    Last Post: 02-23-2003, 03:46 AM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM

Tags for this Thread