English 中文(简体)
c) 病媒推广
原标题:vector replication in c
  • 时间:2012-04-24 17:50:08
  •  标签:
  • c

如何界定诸如病媒之类的一系列工作? 这一阵列应具有任何价值。 它可以具有0项价值或10项价值或100项价值。

下面的法典是行之有效的,但给我留下了 st倒的错误。

int i = 0;
    int* aPtr = &i;
    int* head = aPtr;
    for(i=0;i<6;i++){
        (*aPtr)=i;
        aPtr++;
    }

同样,如何使用<代码>char* str,以达到任何性质,随后无效,最终加以扼杀?

采访实践:

最佳回答

There are many ways to do this in C, depending on your requirements, but you said "any number of values" (which usually means as many as will fit in memory). That s commonly done using realloc to grow the size of an array dynamically. You ll need to keep some bookkeeping information too on the size of the array as it grows.

void
store (vector_t * v, int idx, int value)
{
  if (v->size < idx) {
     v->size = idx * 2;
     v->data = realloc(v->data, v->size);
  }
  v->data[idx] = value;
}

这被打上了“家务劳动”,我留下了一些细节,以填补病媒的定义。

问题回答

在座椅上,在第一次播音之后,你试图进入一个预留地点,而这个地点以前没有宣布或保留。 在第一个版本中,<代码>int i的确为您分配了记忆。

What you could do though would be to initally allocate the memory required using malloc . Once this memory is allocated , and if you walk through only the allocated stack space, you wont come across a run time error.

PS:Your代码如果只是汇编而行不通。 任何方案都可能包含操作时间,并汇编时间错误。 你的代码样本是常年错误的一个非常常见的例子。

这太困难。 必须记住的是,你首先需要用小孔(......)或电离层(......)为你的阵列分配记忆。 之后,你可以很容易地分配(或处理)记忆,作为添加或删除的项目。 动态添加或删除记忆的方法(用于储存阵列中的物品)是真实的(......)。 C Dynamic Deposit的实际信息量相当。 我举了一个例子,说明如何先分配一个果园阵列,然后增加面积和缩小面积。

#include "stdio.h"
#include "stdlib.h"

int main()
{
  char *myDynamicString;

  /* allocate initial memory */
  myDynamicString = (char *)malloc(sizeof(char) * 2);
  myDynamicString[1] =   ;

  /* set values */
  myDynamicString[0] =  A ;

  /* prints: A */
  printf("String: %s
", myDynamicString);

  /* make string bigger */
  myDynamicString = (char *)realloc(myDynamicString, sizeof(char) * 6);
  myDynamicString[5] =   ;

  /* set values */
  myDynamicString[1] =  P ;
  myDynamicString[2] =  P ;
  myDynamicString[3] =  L ;
  myDynamicString[4] =  E ;

  /* prints: APPLE */
  printf("Bigger String: %s
", myDynamicString);

  /* make string smaller */
  myDynamicString = (char *)realloc(myDynamicString, sizeof(char) * 3);
  myDynamicString[2] =   ;

  /* set values */
  myDynamicString[1] =  Z ;

  /* prints: AZ */
  printf("Smaller String: %s
", myDynamicString);

  /* don t forget to release the memory */
  free(myDynamicString);

  return 0;
}




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签