佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 3420|回复: 66

C++ programming 高手请进来帮忙。

[复制链接]
发表于 10-9-2008 11:00 AM | 显示全部楼层 |阅读模式
char* ptaP="abcde";
    char ptaA[sizeof(ptaP)];
    strcpy(ptaA,ptaP);    //Fail


有什么方法可以把Pointer value放进array?

[ 本帖最后由 1max1 于 22-6-2009 03:46 PM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

发表于 10-9-2008 01:25 PM | 显示全部楼层
回复

使用道具 举报

 楼主| 发表于 10-9-2008 06:48 PM | 显示全部楼层
原帖由 jasonmun 于 10-9-2008 01:25 PM 发表
http://www.ggv.com.cn/forum/clib/alloc/malloc.html

用google啦..

还是不很明白,能多举些例子吗?
回复

使用道具 举报

 楼主| 发表于 11-9-2008 06:34 PM | 显示全部楼层
size_t fread(void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file);
              
size_t fwrite(const void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file);

可以帮忙举些例子吗?
回复

使用道具 举报

发表于 12-9-2008 10:09 AM | 显示全部楼层
malloc = make allocation
就是说要开用多少memory给那个pointer.


不知道对没有。。。
回复

使用道具 举报

发表于 12-9-2008 10:35 AM | 显示全部楼层
malloc = memory allocation
回复

使用道具 举报

Follow Us
 楼主| 发表于 12-9-2008 10:45 AM | 显示全部楼层
#include <stdlib.h>

struct node {
  int x;
  struct node *next;
};

int main()
{
    /* This will be the unchanging first node */
    struct node *root;      

    /* Now root points to a node struct */
    root = malloc( sizeof(struct node) );

    /* The node root points to has its next pointer equal to a null pointer
       set */
    root->next = 0;  
    /* By using the -> operator, you can modify what the node,
       a pointer, (root in this case) points to. */
    root->x = 5;     
}

root是个pointer所以指的是struct node的地址 。
不明白的地方是做么还要用malloc( sizeof(struct node) ); ,root的地址反正不就是struct node吗?

   root = malloc( sizeof(struct node) ); 是否和  root = malloc( sizeof(root) );一样意思?

[ 本帖最后由 1max1 于 12-9-2008 10:48 AM 编辑 ]
回复

使用道具 举报

发表于 12-9-2008 11:31 AM | 显示全部楼层
不一样。 root 是一个 pointer,指向一个 struct node 的 structure。在 32bit 系统里面,root的大小是 4 bytes。Struct node 是一个 structure,其大小是一个 int + 一个 struct node pointer, 大约是 8 bytes (在 32bit 环境下,int = 32bit,memory address, 也就是 struct node pointer,也是 32bit,so,加起来 = 64bit = 8 bytes)。

记住:pointer size =/= data size!!!!

[ 本帖最后由 geekman 于 12-9-2008 11:32 AM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 12-9-2008 01:05 PM | 显示全部楼层
原帖由 geekman 于 12-9-2008 11:31 AM 发表
不一样。 root 是一个 pointer,指向一个 struct node 的 structure。在 32bit 系统里面,root的大小是 4 bytes。Struct node 是一个 structure,其大小是一个 int + 一个 struct node pointer, 大约是 8 bytes (在 ...
记住:pointer size =/= data size!!!!

对!对!对!忘了在那里看过。
回复

使用道具 举报

 楼主| 发表于 12-9-2008 01:32 PM | 显示全部楼层
#include<stdio.h>
void printnum (int begin);
int begin=0;

void main()
{
printnum (begin );
}
void printnum ( int begin )
{
printf( "%d", begin );
if ( begin < 9 )
{
printnum ( begin + 1 );
}
printf( "%d", begin );
}

Output:
01234567899876543210Press any key to continue . . .

从0至9是因begin + 1,但9至0是从哪里飞来的,都没有begin - 1?

[ 本帖最后由 1max1 于 19-9-2008 12:09 AM 编辑 ]
回复

使用道具 举报

发表于 12-9-2008 02:04 PM | 显示全部楼层
1)首先,请学习使用正确的 indentation,和学习如何使用正确的 coding style,好让其他人能够更容易的阅读你的源代码。我花了很大的劲才强迫自己看完你的代码。以后再有这种十分费劲的情形,很抱歉,我将会无视,让别人来帮你吧。写program并不是要追求在最少的行数里面写完你的code,而是如何在整齐又容易阅读的情形下,写出你的程式逻辑。当你在一个 project team 里面工作时,你的 coding style 将会让你成为办公室里面最不受欢迎的人。

2)为何要使用 recursive function call,而不干脆使用for loop? Recursive 通常会隐藏着一些危险的陷阱,所以并不应该被滥用,就比如你的这个程式一样,你没注意到你的 printnum() 里面有两个 printf() 吧?(当然,你那难以阅读的 coding style 也是功不可没),那你有可曾想过当你使用 recursive call 时,你的程式是如何处理这两个 printf() 的?当你真正的理解了 recursive call 的运作方式,你就知道那多出来的数字来自哪里了。(提示:一个 printf() 在 recursive call 之前,一个在后,so,当你的 recursive call return 的时候就会碰到另一个。。。recursive 是需要收拾手尾的。。。)

这问题就留给你去思考把,顺便让你了解到 recursive call 的危险和使用上的真正意义。
回复

使用道具 举报

 楼主| 发表于 12-9-2008 10:37 PM | 显示全部楼层
原帖由 geekman 于 12-9-2008 02:04 PM 发表
1)首先,请学习使用正确的 indentation,和学习如何使用正确的 coding style,好让其他人能够更容易的阅读你的源代码。我花了很大的劲才强迫自己看完你的代码。以后再有这种十分费劲的情形,很抱歉,我将会无视,让 ...

原来是这样,谢谢指点!
回复

使用道具 举报

发表于 13-9-2008 09:38 PM | 显示全部楼层
malloc 允许你 使用 heap memory,

一般的 local variable 都是用那有限的 stack memory 。。
回复

使用道具 举报

发表于 17-9-2008 11:22 AM | 显示全部楼层
C programming不是很难呀!
回复

使用道具 举报

发表于 17-9-2008 12:50 PM | 显示全部楼层
忽然觉得吹来一阵凉凉的清风。。。
回复

使用道具 举报

 楼主| 发表于 17-9-2008 08:04 PM | 显示全部楼层
原帖由 大_雄 于 17-9-2008 11:22 AM 发表
C programming不是很难呀!


/* Fig. 12.3: fig12_03.c
   Operating and maintaining a list */
#include <stdio.h>
#include <stdlib.h>

/* self-referential structure */
struct listNode {            
   char data; /* each listNode contains a character */
   struct listNode *nextPtr; /* pointer to next node*/
}; /* end structure listNode */

typedef struct listNode ListNode; /* synonym for struct listNode */
typedef ListNode *ListNodePtr; /* synonym for ListNode* */

/* prototypes */
void insert( ListNodePtr *sPtr, char value );
char delete( ListNodePtr *sPtr, char value );
int isEmpty( ListNodePtr sPtr );
void printList( ListNodePtr currentPtr );
void instructions( void );

int main( void )
{
   ListNodePtr startPtr = NULL; /* initially there are no nodes */
   int choice; /* user's choice */
   char item;  /* char entered by user */

   instructions(); /* display the menu */
   printf( "? " );
   scanf( "%d", &choice );

   /* loop while user does not choose 3 */
   while ( choice != 3 ) {

      switch ( choice ) {

         case 1:
            printf( "Enter a character: " );
            scanf( "\n%c", &item );
            insert( &startPtr, item ); /* insert item in list */
            printList( startPtr );
            break;

         case 2:

            /* if list is not empty */
            if ( !isEmpty( startPtr ) ) {
               printf( "Enter character to be deleted: " );
               scanf( "\n%c", &item );

               /* if character is found, remove it */
               if ( delete( &startPtr, item ) ) { /* remove item */
                  printf( "%c deleted.\n", item );
                  printList( startPtr );
               } /* end if */
               else {
                  printf( "%c not found.\n\n", item );
               } /* end else */

            } /* end if */
            else {
               printf( "List is empty.\n\n" );
            } /* end else */

            break;

         default:
            printf( "Invalid choice.\n\n" );
            instructions();
            break;
      
      } /* end switch */

      printf( "? " );
      scanf( "%d", &choice );
   } /* end while */

   printf( "End of run.\n" );
   
   return 0; /* indicates successful termination */

} /* end main */

/* display program instructions to user */
void instructions( void )
{
   printf( "Enter your choice:\n"
      "   1 to insert an element into the list.\n"
      "   2 to delete an element from the list.\n"
      "   3 to end.\n" );
} /* end function instructions */

/* Insert a new value into the list in sorted order */
void insert( ListNodePtr *sPtr, char value )
{
   ListNodePtr newPtr;      /* pointer to new node */
   ListNodePtr previousPtr; /* pointer to previous node in list */
   ListNodePtr currentPtr;  /* pointer to current node in list */

   newPtr = malloc( sizeof( ListNode ) ); /* create node */

   if ( newPtr != NULL ) { /* is space available */
      newPtr->data = value; /* place value in node */
      newPtr->nextPtr = NULL; /* node does not link to another node */

      previousPtr = NULL;
      currentPtr = *sPtr;

      /* loop to find the correct location in the list */
      while ( currentPtr != NULL && value > currentPtr->data ) {
         previousPtr = currentPtr;          /* walk to ...   */
         currentPtr = currentPtr->nextPtr;  /* ... next node */
      } /* end while */

      /* insert new node at beginning of list */
      if ( previousPtr == NULL ) {
         newPtr->nextPtr = *sPtr;
         *sPtr = newPtr;
      } /* end if */
      else { /* insert new node between previousPtr and currentPtr */
         previousPtr->nextPtr = newPtr;
         newPtr->nextPtr = currentPtr;
      } /* end else */
   
   } /* end if */
   else {
      printf( "%c not inserted. No memory available.\n", value );
   } /* end else */

} /* end function insert */

/* Delete a list element */
char delete( ListNodePtr *sPtr, char value )
{
   ListNodePtr previousPtr; /* pointer to previous node in list */
   ListNodePtr currentPtr;  /* pointer to current node in list */
   ListNodePtr tempPtr;     /* temporary node pointer */

   /* delete first node */
   if ( value == ( *sPtr )->data ) {
      tempPtr = *sPtr; /* hold onto node being removed */
      *sPtr = ( *sPtr )->nextPtr; /* de-thread the node */
      free( tempPtr ); /* free the de-threaded node */
      return value;
   } /* end if */
   else {
      previousPtr = *sPtr;
      currentPtr = ( *sPtr )->nextPtr;

      /* loop to find the correct location in the list */
      while ( currentPtr != NULL && currentPtr->data != value ) {
         previousPtr = currentPtr;         /* walk to ...   */
         currentPtr = currentPtr->nextPtr; /* ... next node */  
      } /* end while */

      /* delete node at currentPtr */
      if ( currentPtr != NULL ) {
         tempPtr = currentPtr;
         previousPtr->nextPtr = currentPtr->nextPtr;
         free( tempPtr );
         return value;
      } /* end if */
     
   } /* end else */

   return '\0';

} /* end function delete */

/* Return 1 if the list is empty, 0 otherwise */
int isEmpty( ListNodePtr sPtr )
{
   return sPtr == NULL;

} /* end function isEmpty */

/* Print the list */
void printList( ListNodePtr currentPtr )
{

   /* if list is empty */
   if ( currentPtr == NULL ) {
      printf( "List is empty.\n\n" );
   } /* end if */
   else {
      printf( "The list is:\n" );

      /* while not the end of the list */
      while ( currentPtr != NULL ) {
         printf( "%c --> ", currentPtr->data );
         currentPtr = currentPtr->nextPtr;   
      } /* end while */

      printf( "NULL\n\n" );
   } /* end else */

} /* end function printList */


做么newptr 有自己的memory address,而previousptr 和currentptr没有?
意思是说previousptr 和currentptr 分享同样的address与value,更改previousptr的data与nextptr(previousptr->data, previousptr->nextptr)也就更改currentptr(currentptr->data, currentptr->nextptr)?


[ 本帖最后由 1max1 于 20-9-2008 03:15 PM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

发表于 18-9-2008 10:39 PM | 显示全部楼层
原帖由 1max1 于 12-9-2008 01:32 PM 发表

从0至9是因begin + 1,但9至0是从哪里飞来的,都没有begin - 1?


你用到了recuisive,
你把begin<9設成<3
然後自己要跟著程式的function
一直call下去,
就會知道它是怎麼印出來的
不要忘記你在那個function裡面用了兩次printf
回复

使用道具 举报

发表于 18-9-2008 10:41 PM | 显示全部楼层
http://squall.cs.ntou.edu.tw/cpr ... moryAllocation.html

去看看吧,
關於malloc動態配置記憶體

自己可以用小程式來玩玩malloc
回复

使用道具 举报

 楼主| 发表于 19-9-2008 12:16 AM | 显示全部楼层
你用到了recuisive,
你把begin<9設成<3
然後自己要跟著程式的function
一直call下去,
就會知道它是怎麼印出來的
不要忘記你在那個function裡面用了兩次printf

是咯,第一个printf会重复print 9次(1-9),因printnum(begin+1)funtion call。
但是第二个 printf 就没见得有任何repeat的funtion call,因该只是个9,怎么会从9 print 直1,9次。

http://squall.cs.ntou.edu.tw/cpr ... moryAllocation.html

去看看吧,
關於malloc動態配置記憶體

自己可以用小程式來玩玩malloc

呵呵,明白了,谢谢哦。
回复

使用道具 举报

 楼主| 发表于 19-9-2008 11:38 AM | 显示全部楼层
if ( previousPtr == NULL ) {
         newPtr->nextPtr = *sPtr;
         *sPtr = newPtr;
      } /* end if */
      else { /* insert new node between previousPtr and currentPtr */
         previousPtr->nextPtr = newPtr;
         newPtr->nextPtr = currentPtr;
      } /* end else */


pointer是address不是value,做么不直接
newPtr->nextPtr = newPtr;,previousPtr->nextPtr = currentPtr;
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 8-12-2025 12:30 PM , Processed in 0.203497 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表