glib 자료구조 중에 gptrarray 에 대해 질문 있습니다 !!

erich0929의 이미지

자꾸 세그멘테이션 오류가 뜨네요.
gdb로 확인해보니 g_ptr_array_index 로 얻어온 element 포인터를 DATA * 로 캐스팅해서 필드 값을 참조할 때 오류가 났습니다.
구글링을 해도 참고할 만한 자료가 없네요..
처음 글 올려 봅니다...
도와주세요,,.,

  1 #include <stdio.h>
  2 #include <glib.h>
  3 
  4 typedef struct _DATA {
  5         int item;
  6         char* name;
  7         int price;
  8         double value;
  9 } DATA;
 10 
 11 int compare_items (gpointer, gpointer);
 12 void printAll (gpointer, gpointer);
 13 
 14 DATA* data [] = {{7143, "A", 1231234, 235.5},
 15                 {5231, "B", 532458, 238.75},
 16                 {8751, "C", 872654, 125.47}};
 17 
 18 int main(void)
 19 {
 20     GPtrArray* a = g_ptr_array_new ();
 21     g_ptr_array_add (a, data[0]);
 22     g_ptr_array_add (a, data[1]);
 23     g_ptr_array_add (a, data[2]);
 24 
 25     /* g_ptr_array_sort (a, (GCompareFunc) compare_items); */ 
 
 26     DATA* temp = (DATA*) g_ptr_array_index (a, 1); 
 27     int k = temp -> item; 
 28     /* printf ("item : %d\n", temp -> item); */ 
 29  
 30     /* g_ptr_array_foreach (a, printAll, (gpointer) NULL); */ 
 31  
 32     return 0; 
 33 } 
 34  
 35 int compare_items (gpointer a, gpointer b) { 
 36         DATA* alpha = (DATA *) a; 
 37         DATA* beta = (DATA *) b; 
 38  
 39         return alpha->item - beta->item; 
 40 } 
 41  
 42 void printAll (gpointer a, gpointer b) { 
 43         DATA* alpha = (DATA*) a; 
 44         printf ("item : %d, name : %s, price : %d, value : %lf\n", alpha -> item, alpha -> name, alpha -> price, alpha ->    value); 
 45         return; 
 46 }
klara의 이미지

적으신 코드에서 14줄의 data의 우변은 DATA*[]이 아니라 DATA[]입니다.

erich0929의 이미지

ㅠㅠ 진짜 이거 하나 때문에 세시간 삽질 했네요 ~ ^^
복 많이 받으세요~^^

Make it a better place...

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.