这段代码有什么问题(正确打印arr [0]但是我遇到arr [1]的问题...打印一些奇怪的字符):
using namespace std;
char ** setName() {
char * arr[2];
for (int i=0;i<2;i++)
arr[i] = (char*)malloc(100);
arr[0] = strdup("Robert");
arr[1] = strdup("Jose");
return arr;
}
int main()
{
char **arr;
arr = setName();
printf("First name is %s\n", arr[0]);
printf("Second name is %s\n", arr[1]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果重要,我使用Visual Studio 8在Windows中运行此代码.
有一个作业,我应该在其中创建指向对象的指针的向量
稍后,我将使用继承/多态性来扩展类,以包括两天交付,第二天空运等费用。但是,现在我不在乎。当前程序的最终目标是仅打印出矢量中每个对象的内容(名称和地址)并找到其运输成本(重量*成本)。
我的麻烦与逻辑不符,我只是对与对象/指针/向量有关的几点感到困惑。但是首先我的代码。我基本上删去了目前不重要的所有内容,int main将具有用户输入,但是现在我硬编码了两个示例。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Package {
public:
Package(); //default constructor
Package(string d_name, string d_add, string d_zip, string d_city, string d_state, double c, double w);
double calculateCost(double, double);
~Package();
private:
string dest_name;
string dest_address;
string dest_zip;
string dest_city;
string dest_state;
double weight;
double cost;
};
Package::Package()
{
cout<<"Constucting Package Object with default values: "<<endl;
string dest_name="";
string dest_address="";
string dest_zip="";
string dest_city="";
string dest_state="";
double weight=0;
double cost=0;
}
Package::Package(string d_name, …Run Code Online (Sandbox Code Playgroud) 使用LinkedList实现的Stack抽象数据类型的put(x)和get()函数的时间复杂度是多少?
我的第一个想法是他们都是O(1).但是如果get()必须从头节点遍历到列表中的最后一个元素以找到要删除和返回的元素,则get()函数将为O(n).
put(x)函数也必须遍历整个列表以找到最后一个节点,它将安装一个新节点.所以这也是O(n).
如果使用LinkedList的"专用"版本,一个始终保持指向列表中最后一个节点的指针,则这两个版本都将成为恒定时间操作.我是否正确理解LinkedList的标准实现不具备此功能?
stack pointers abstract-data-type time-complexity data-structures
我有一个从函数返回的char指针,我想使用IF语句仔细检查它的值我应该如何在C中实现这个.
substring函数来自这里
我想要这样的东西
if (substring("xxxNameYYY",0,3)=="xxx"){
//do this
} else {
//do that
}
Run Code Online (Sandbox Code Playgroud)
以上if语句总是去"做那个"部分.
谢谢
我总是怀疑这个疑问.请看以下程序:
#include <stdio.h>
char * function1(void);
int main()
{
char *ch;
ch = function1();
printf("hello");
free(ch);
return 0;
}
char* function1()
{
char *temp;
temp = (char *)malloc(sizeof(char)*10);
return temp;
}
Run Code Online (Sandbox Code Playgroud)
我在这里泄漏记忆吗?该程序不会因为某些警告而崩溃:
prog.c: In function ‘main’:
prog.c:11: warning: implicit declaration of function ‘free’
prog.c:11: warning: incompatible implicit declaration of built-in function ‘free’
prog.c: In function ‘function1’:
prog.c:19: warning: implicit declaration of function ‘malloc’
prog.c:19: warning: incompatible implicit declaration of built-in function ‘malloc’
Run Code Online (Sandbox Code Playgroud)
打印你好.
我只是C.so的初学者请帮助我理解在function1.does中返回语句之后发生了什么真的释放了funtion1中分配的内存?
假设:
6 char arr[] = "ABC";
7
8 char *ptr = &arr;
9 char *ptr2 = &ptr;
Run Code Online (Sandbox Code Playgroud)
使用ptr2,我如何访问元素c?
我本以为会有以下方法可行,但......不然.
**ptr2[1]
我通常避免使用类指针,因为引用似乎更有效.但我最近被迫使用它们,因为它们提供了唯一(高效且简单)的解决方案,在windows api包装器中使用window id绑定函数.我创建了一个WinControl类的数组.在我的WinHandler类中,它处理WndProc(窗口过程)并将程序中使用的所有小部件添加到该数组中.
class WinControl //These are not the entire classes, just the significant parts.
{
public:
int WinID;
virtual void Click(void) = 0; //Pure Virtual Function.
}
class WinHandler
{
WinHandler() : WCount(0) { }
WinControl* WidgetSet[MAX_LENGTH]; // Or I can use an STL vector...
int WCount;
void AddWidget(Widget* w) { WCount++; WidgetSet[WCount] = w; }
}
Run Code Online (Sandbox Code Playgroud)
然后我用:
if (WidgetSet[i]->ID == LOWORD(wParam)) WidgetSet[i]->Click();
Run Code Online (Sandbox Code Playgroud)
从长远来看,这种做法是否正常?由于实际存储在WidgetSet中的对象都是WinControl类的衍生物.有谁能建议更好的方法?
注意:我试图让我的问题尽可能清楚.如果你仍然无法得到我所要求的,请发表评论,我将尝试详细说明这个问题.
在以下程序中:
int main(){
int i = 0x52554153;
const char *s = reinterpret_cast<const char*>(&i);
std::string *s1 = new std::string(s);
std::cout<<"word is "<<*s1<<std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我将4字节int转换为4字节"const char"字符串.我使用的整数值只是字符串"RUAS"的ascii值的集合.我期待在"cout"声明中得到相同的结果.但它打印"SAUR",这与我提供的完全相反.请解释我的输出.
我正在尝试构建一个m-way树,我无法可视化指向B_tree节点类的不同实例的指针数组(这基本上创建了数组类型节点,并包括与树相关的所有函数,如count,insert等等)
对于这种情况,是否有任何提示/技巧可视化指针数组?是否有任何好的链接/资源来解释指针数组?(我没有在谷歌上找到有用的常见搜索结果)...
如果我有以下例子:
int *x;
x = func(a);
Run Code Online (Sandbox Code Playgroud)
对于声明:x = func(a);,我们是否说我们要回复一个地址x?或者,我们究竟如何阅读它?
编辑:是否有资格说我们正在返回指针x?如果是这样,你能解释一下这是如何完成的吗?我的意思是,我们如何返回指针?