我有一些C代码,我想移植到java.我没有做太多的C编码,但我能够跟进,直到这个功能.如果有人能帮助我了解正在发生的事情,我将不胜感激.
int reverse_integer(int input) {
int output = 0, i;
for ( i=0, i<sizeof(int); i++ ) {
output = ( input & 0x000000FF ) | output;
input >>= 8;
if ( i < 3 ) {
output <<= 8;
}
}
return output;
}
Run Code Online (Sandbox Code Playgroud)
该函数用作:
char * position = //some data
/*the included comment on this next line states its the size of a string*/
int i = reverse_integer( *(int*)position )
Run Code Online (Sandbox Code Playgroud) 我正在看这个读取输入行的程序,然后从K&R对它们进行排序.
如果我输入的话,我无法弄清楚为什么它没有正确排序
1234532 first line
abc second line
Run Code Online (Sandbox Code Playgroud)
它不会按递增顺序对它们进行排序.基本上,如果输入行包含数字或其他字母,我认为它不起作用.
但这适用于带字母的行:
abc
abcsda
Run Code Online (Sandbox Code Playgroud)
将正确排序.
这是代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLINES 5000
char *lineptr[MAXLINES];
int readlines(char *lineptr[], int nlines);
void writelines(char *lineptr[], int nlines);
void qsort(char *lineptr[], int left, int right);
int main(int argc, char *argv[])
{
int nlines;
if((nlines = readlines(lineptr, MAXLINES)) >= 0) {
qsort(lineptr, 0, nlines-1);
writelines(lineptr, nlines);
system("PAUSE");
return 0;
}
else {
printf("error: input too big to sort\n");
return 1;
}
system("PAUSE");
return 0; …Run Code Online (Sandbox Code Playgroud) 以下代码在第二次Pop()调用时崩溃.我是C的新手,我现在一直盯着这段代码超过一个小时,我看不出错误.任何想法,以帮助我解释为什么这个代码崩溃?
#include <stdio.h>
#define StackDataSize 100
typedef struct Stack
{
int index;
void *data[StackDataSize];
} Stack;
void* Pop(Stack *s)
{
if(s->index >= 0)
{
return s->data[s->index--];
}
else
{
fprintf(stderr, "ERROR: Stack Empty\n");
return NULL;
}
}
void Push(Stack *s, void *v)
{
if(s->index < StackDataSize)
{
s->data[++s->index] = v;
}
else
{
fprintf(stderr, "ERROR: Stack Full\n");
}
}
int main(void)
{
Stack s = {-1}, *intstack = &s;
int x = 123456;
Push(intstack, &x);
printf("%d\n", *(int*)Pop(intstack));
printf("%d\n", …Run Code Online (Sandbox Code Playgroud) 我一直在阅读关于参考资料的PHP手册,有些东西让我感到困惑.它说引用不是指向内存地址的指针而是......
相反,它们是符号表别名.
如果引用指向符号表条目然后指向内存地址,这本质上不是指针吗?
编辑:
一些很棒的答案.只想在这里弹出这个...我怎样才能取消另一个指向的变量?
$var = "text";
$ref =& $var;
unset($ref);
Run Code Online (Sandbox Code Playgroud)
看起来这个工作,我需要取消设置$var,以便GC删除它.
可能重复:
"int i"和"int i" 之间有什么区别 ?
const int*,const int*const,int const*之间的区别是什么?
有什么区别
char* getInput();
Run Code Online (Sandbox Code Playgroud)
和
char *getInput();
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
int main() {
char *sPPhrase[51];
/* Input */
printf("Enter string (max. 50 chars):\n");
fflush(stdout); /* Works around an annoying Eclipse bug that fails to display the output from the printf command */
scanf("%s", *sPPhrase); /* Won't work */
/* More code goes here */
}
Run Code Online (Sandbox Code Playgroud)
该scanf()命令失败,我想,是因为*sPPhrase不可写的sPPhrase指向一个字符串常量.编译器没有任何错误的线索.稍后,我需要将此字符串传递给此函数:
char* reverse(char* sPPhrase[]);
字符串常量不可写,但我需要将此char*传递给此函数.如何重写我的代码以使其工作?
在Effective C++的第42页上,指针用作数组名称ala
AirPlane*newBlock = ...
newBlock [I]的.next = 0;
我没有意识到这是合法的.这是c ++标准的一部分吗?这是常见做法吗?
我有一个较早的帖子链接文本,有人说我的指针初始化为错误的元素,我不明白为什么,除了他们是正确的,它适用于他们的纠正.所以这是基本问题:
如果我声明一个0到30之间的数组
#define ENDPOINT 15
int record[2 * ENDPOINT + 1];
// and want a pointer to be at the middle of the array, so 0 is at the middle, and
// +15 is at 30, and -15 is at 0
// why is int *ptr = &record[ENDPOINT + 1] wrong? why is the correct declaration
int *ptr = &record[ENDPOINT];
Run Code Online (Sandbox Code Playgroud)
因为如果我将ptr放在&record [ENDPOINT],这意味着记录数组中第15个条目是第14个索引,然后添加15将只有29个对吗?谢谢!
我正在尝试使用一系列char指针.
假设我动态地声明这样的数组:
int numrows=100;
char** array = new char*[numrows];
Run Code Online (Sandbox Code Playgroud)
然后我通过使用getline从文件中获取字符串,将字符串转换为char数组,然后在我的数组中设置指针指向所述char数组来填充它,如下所示:
string entry;
int i=0;
while (getline(file,entry)){
char* cstring = new char[entry.length()];
array[i]=strncpy(cstring,entry.c_str(),entry.length());
free(cstring);
i++;
}
Run Code Online (Sandbox Code Playgroud)
(这有效,但是有更好的方法吗?)
问题是,一旦我变得大于numrows,我不知道如何增长数组.我知道如何为一维数组做这个,但是二维性让我失望.
我想我应该能够像增长一维阵列一样成长它,对吗?
if (i==numrows){
char** temp = new char*[numrows+numrows];
for (int j=0;j<i;j++){
char* cstring = new char[strlen(array[i])];
temp[i]=strncpy(cstrin,array[i],strlen(array[i]));
free(cstring);
}
delete [] array;
array = temp;
}
Run Code Online (Sandbox Code Playgroud)
因此,如果当前数组变满,则创建第二个数组,该数组的大小是当前数组的两倍,并用当前数组的内容填充它.然后删除数组,让数组指向temp.我很高兴让temp成为新阵列.我可以将数组的内容转换为temp,但是当我删除数组并设置array = temp时,数组的内容不是temp的内容.
所以我的问题是我怎样才能/应该发展这个动态数组的char指针?
从这段代码:
int x = 5;
int other = 10;
vector<int*> v_ptr;
v_ptr.push_back(&x);
v_ptr.push_back(&other);
v_ptr.push_back(&x);
Run Code Online (Sandbox Code Playgroud)
无论如何,我可以x从x变量本身知道谁指出了什么,这样我就不必在里面搜索v_ptr地址了x?这在C++或C++ 0x中是否可行?
我读到在进行垃圾收集时,他们查看内存并查看是否有任何指向它,然后决定删除未使用的变量等.