?
Solved

memset: need & operator for strings?

Posted on 2006-06-05
3
Medium Priority
?
442 Views
Last Modified: 2008-03-10
If I am using memset() to set a string to nulls
(in particular a string within a structure)

typedef struct
{
    long id_code;
    char last_name[31];
    char first_name[21];
    /* etc */
}

I always forget if I need the address (&) operator.
In other words,
which of the following to statements is preferred:

memset(person.last_name, 0, sizeof(person.last_name));
memset(&person.last_name, 0, sizeof(person.last_name));

My gut feeling is that it does not matter for a string
but would appreciate any feedback.

Thanks,
Steve
0
Comment
Question by:Stephen Kairys
3 Comments
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 200 total points
ID: 16835329
memset(person.last_name, 0, sizeof(person.last_name));

This is right version. In C string is pointer: last_name points to first bytes of memory region. Non-pointer types require &.
0
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 200 total points
ID: 16835588
Hi stevefromc,

No matter whether the object is in a union, struct, globals block, stack, or anywhere else, the rule is simple.  If the object is defined as an array or a pointer, it is a pointer and the '&' symbol isn't needed.  If the object is a scalar variable, then the '&' is needed.


Good Luck!
Kent
0
 
LVL 4

Author Comment

by:Stephen Kairys
ID: 16835937
Thanks to both of you. Accepted answer goes to Kent as per his more detailed response.

Steve
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question