c언어 다중 Struct의 값을 지우는 문제!!! 도와주세요

sim8826의 이미지

다음과 같은 Nested Structure가 있는데요.. 요약하면.. Contact라는 스트럭트가 Name과 Numbers라는 스트럭트를 가지고있고, 해당 스트럭트들은 다시 문자열이나 int값을 가지고 있습니당..

struct Name {
    char firstName[31];
    char lastName[36];
    int age;
};
 
struct Numbers {
    char cell[15];
    char home[15];
};
 
struct Contact {
    struct Name name;
    struct Numbers numbers;
};

제가 하고 싶은 것은.. Contact 변수를 통해서 Name안에 있는 2개의 문자열과 1개의 int를 전부 0으로 돌려주고 싶습니다.

먼저 Contact 타입의 contact라는 변수를 초기화 해볼게여..

struct Contact contact[2] =
{ {"Jay", "Greene", 20}, {14, 15},
  {"Jay2", "Greene2", 23}, {16, 17} }; 

여기까진 괜찮은데.. 그 다음에 contact[0].name의 값들을 전부 없애주고 싶은데.. 일일히 접근하는거 말고 '한방에' 처리하는게 가능한가요..?

현재 제 지식으로는 Null terminator를 활용해서 이렇게는 할 수 있습니다.

printf("%s \n\n", contact[0].name.firstName); //output: Jay
 
contact[0].name.firstName[0] ='\0'; 
contact[0].name.lastName[0] ='\0';
contact[0].name.age = 0;
 
printf("%s", contact[0].name.firstName); //no output here.

그런데 이렇게 코드를 쓰면 Name 스트럭트 안에 너무 배열이 많으면 너무 번거로우니..

그냥 contact[0].name이라는 접근자만 사용해서 하위 배열인 firstName, lastName, age까지 죄다 삭제해버리고 싶거든요...

저의 모든 지식과 상상력을 동원해서 가능한 모든코드를 다해본 것 같은데도 전부다 컴파일 에러입니다. stack overflow에서 찾은 방법도 안되요. 이것만 생각하면 잠이 안옵니다..

//컴파일에러!!!
contact[0].name={0};
contact[0].name={{0}, 0};
contact[0].name=0;
contact[0].name={{0},{0}, 0};

전부 다 컴파일 에러에요... 이런식의 방식은 C언어가 지원하지 않는건가요? 그냥 Name 스트럭트 변수를 따로 선언해서 코딩해야 하는것인지.. 도대체 뭐가 문제인지 도저히 모르겠습니다..

익명 사용자의 이미지

bzero(&contact[0].name, sizeof(struct Name))
memset(&contact[0].name, 0, sizeof(struct Name));

둘 중 아무거나 써도 됩니다. 단 Name 구조체에 포인터 변수가 들어간다면 위 방법을 사용할 수 없습니다.

//컴파일에러!!!
contact[0].name={0};
contact[0].name={{0}, 0};
contact[0].name=0;
contact[0].name={{0},{0}, 0};

저거는 선언할 때에만 사용할 수 있습니다.
라스코니의 이미지

음. 저라면 resetNameStruct 함수를 만들어서 사용하겠습니다.
또는 inline이나 define으로도 할 수 있을 겁니다.

void resetNameStruct(struct Name *obj)
{
   assert(obj);
   obj->firstName[0] ='\0'; 
   obj->lastName[0] ='\0';
   obj->age = 0;
}
 
#define resetNameStruct(obj) \
   do { \
      obj->firstName[0] ='\0'; \
      obj->lastName[0] ='\0'; \
      obj->age = 0; \
   } while (0)
 
resetNameStruct(&contact[0].name);
익명 사용자의 이미지

contact[0].name = (struct Name){ 0, };

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.