Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

structure members

Status
Not open for further replies.

mbozza

Programmer
Oct 3, 2003
9
GB
I am having problems accessing certain structure members, namely char* members. Instead of reading along the Char* array, it is incrementing it. What am I doing wrong?

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


#define NAME 1162690894 // read as 4 bytes, spells NAME

void prnt_block( struct data_block*);
void prntNAME(char**);

struct data_block{

char *cname;

};

int main(void)
{
struct data_block data[10];

data[0].cname = (char *)calloc( 4, sizeof( char ) );
data[0].cname = (char *)NAME;

printf(&quot;1st value of cname: %d\n&quot;, data->cname);

// call func
prnt_block(&data[0]);


return 0;

} // end o' main

void prnt_block(struct data_block *db)
{

prntNAME(&(db->cname));

}

void prntNAME(char **n)
{
int i;
printf(&quot;2nd value of cname: %d\n&quot;, *n);

for(i = 0; i < 4; i++)
{
printf(&quot;%c&quot;, *n);
(*n) = (*n) + 1;
}
printf(&quot;\n&quot;);
printf(&quot;3rd value of cname: %d\n&quot;, *n);
}

if you run this it will increment the value of cname, instead of reading along the char* array, printing the byte values as chars. The result is NOPQ instead of NAME, as I want.

 
Try this
Code:
#include <stdio.h> 
#include <stdlib.h>
#include <string.h>

#define NAME 1162690894  // read as 4 bytes, spells NAME

void prnt_block( struct data_block*); 
void prntNAME(char**); 

struct data_block{ 

    char *cname; 

}; 

int main(void) 
{ 
    struct data_block data[10]; 
    unsigned int myname = NAME;

    data[0].cname = (char *)calloc( 4, sizeof( char ) ); 
//!! This doesn't copy to allocated memory, it just overwrites the pointer
//!! data[0].cname = (char *)NAME; 

    // copy the contents of the int to where the memory has been allocated
    memcpy( data[0].cname, &myname, 4 );

//!! better yet, use a string rather than some mystery number
//!! strncpy( data[0].cname, &quot;NAME&quot;, 4 );

//!! even better, dismiss the calloc and do
//!! data[0].cname = &quot;NAME&quot;;

    printf(&quot;1st value of cname: %p\n&quot;, data->cname); //!! Use %p for pointers

    // call func 
    prnt_block(&data[0]); 


    return 0; 

} // end o' main 

void prnt_block(struct data_block *db) 
{ 

    prntNAME(&(db->cname)); 

} 

void prntNAME(char **n) 
{ 
    int i; 
    /* use a temp pointer to store a dereferenced pointer, */
    /* which you can then increment */
    char *p = *n;
    printf(&quot;2nd value of cname: %p\n&quot;, *n); 

    for(i = 0; i < 4; i++) 
    { 
        printf(&quot;%c&quot;, *p); 
        p++;
    } 
    printf(&quot;\n&quot;); 
    printf(&quot;3rd value of cname: %p\n&quot;, *n); 
}

To dereference and index a pointer, you would have to have done this
Code:
  printf(&quot;%c&quot;, (*n)[i] );

--
 
thanks, I think that last bit - (*n); - is the killer line I needed all the time.

thanks once again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top