memset: need & operator for strings?

Posted on 2006-06-05
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.

Question by:Stephen Kairys
    LVL 48

    Assisted Solution

    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 &.
    LVL 45

    Accepted Solution

    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!
    LVL 4

    Author Comment

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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    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 is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now